官术网_书友最值得收藏!

  • C# Programming Cookbook
  • Dirk Strauss
  • 576字
  • 2021-07-14 11:08:42

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:

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.

主站蜘蛛池模板: 将乐县| 田阳县| 嘉义市| 和硕县| 桐庐县| 新建县| 南乐县| 延川县| 彭泽县| 喀什市| 高阳县| 曲周县| 桦川县| 上饶县| 奈曼旗| 大埔县| 阳原县| 建阳市| 屏东市| 河南省| 拜城县| 肥乡县| 无锡市| 南华县| 芜湖县| 兴隆县| 古交市| 长顺县| 太保市| 敖汉旗| 怀仁县| 潜江市| 仙桃市| 格尔木市| 揭东县| 抚松县| 台中县| 吉林省| 阳曲县| 固原市| 新邵县|