Expression-bodied functions and properties

As the name suggests, expression-bodied functions and properties allow methods and properties to have a body that is an expression instead of a statement. You will notice that expression-bodied members look a lot like lambda expressions, because they are inspired by lambda expressions.

Getting ready

To truly appreciate expression-bodied functions and properties, we need to look at the way code had to be written previously. We will create a class to calculate the sale price of an item, and the class will contain two public methods. One will set a shelf price, and the other will return a message displaying the calculated sale price.

How to do it…

  1. Create a class called Recipe6ExpressionBodiedFunctionMembers and add two private auto-implemented properties to hold the sale discount percent and the shelf price:
    public static class Recipe6ExpressionBodiedFunctionMembers
    {
        private static int SaleDiscountPercent { get; } = 20;
        private static decimal ShelfPrice { get; set; } = 100;
    }
  2. If you haven't done so in an earlier recipe, add the extension method class to calculate the sale price of an item:
    public static class ExtensionMethods
    {
        public static decimal CalculateSalePrice(this decimal shelfPrice, int discountPercent)
        {
            decimal discountValue = (shelfPrice / 100) * discountPercent;
            return shelfPrice - discountValue;
        }
    }
  3. We will now add a calculated property to the class. This calculated property uses the extension method on the ShelfPrice property to get the sale price:
    private static decimal GetCalculatedSalePrice
    {
        get { return Math.Round(ShelfPrice.CalculateSalePrice(SaleDiscountPercen t) ,2); } 
    }
  4. Finally, add two methods to your class to set the shelf price and another to return a message with the sale price:
    public static void SetShelfPrice(decimal shelfPrice)
    {
        ShelfPrice = shelfPrice;
    }                
    
    public static string ReturnMessage(string barCode)
    {
        return $"The sale price for barcode {barCode} is {GetCalculatedSalePrice}";
    }
  5. To see the result of the code, add the following code to your console application:
    string BarCode = "12345113";
    decimal ShelfPrice = 56.99m;
    Chapter1.Recipe6ExpressionBodiedFunctionMembers.SetShelfPri ce(ShelfPrice);            Console.WriteLine(Chapter1.Recipe6ExpressionBodiedFunctionM embers.ReturnMessage(BarCode));
    Console.Read();

How it works…

Running your application produces the message displaying the calculated sale price:

How it works…

Note

Here, we are just supplying the bar code in the output message. However, in a live system, the shelf price would be looked up from a data store for the specific bar code.

Looking back at our class, we can see that it is somewhat bulky. We have a calculated property that returns a sale price and two methods with a single return statement. One sets the shelf price, while the other gets a message containing the sale price. This is where expression-bodied function members come into play. Modify your code in the Recipe6ExpressionBodiedFunctionMembers class to make it look like this:

public static class Recipe6ExpressionBodiedFunctionMembers
{
    private static int SaleDiscountPercent { get; } = 20;
    private static decimal ShelfPrice { get; set; } = 100;

    private static decimal GetCalculatedSalePrice => Math.Round(ShelfPrice.CalculateSalePrice(SaleDiscountPercent));

    public static void SetShelfPrice(decimal shelfPrice) => ShelfPrice = shelfPrice;

    public static string ReturnMessage(string barCode) => $"The sale price for barcode {barCode} is {GetCalculatedSalePrice}";        
}

What we are left with is a terse class that does exactly the same as the code we wrote before. There is less code, it is easier to read, and it looks much cleaner. You will notice the use of the lambda => operator. For the GetCalculatedSalePrice computed property, the get keyword is missing. This became implied when we changed the computed property body to an expression.

One point to remember though is that expression-bodied function members do not work with constructors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.188.238