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.
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.
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; }
public static class ExtensionMethods { public static decimal CalculateSalePrice(this decimal shelfPrice, int discountPercent) { decimal discountValue = (shelfPrice / 100) * discountPercent; return shelfPrice - discountValue; } }
ShelfPrice
property to get the sale price:private static decimal GetCalculatedSalePrice { get { return Math.Round(ShelfPrice.CalculateSalePrice(SaleDiscountPercen t) ,2); } }
public static void SetShelfPrice(decimal shelfPrice) { ShelfPrice = shelfPrice; } public static string ReturnMessage(string barCode) { return $"The sale price for barcode {barCode} is {GetCalculatedSalePrice}"; }
string BarCode = "12345113"; decimal ShelfPrice = 56.99m; Chapter1.Recipe6ExpressionBodiedFunctionMembers.SetShelfPri ce(ShelfPrice); Console.WriteLine(Chapter1.Recipe6ExpressionBodiedFunctionM embers.ReturnMessage(BarCode)); Console.Read();
Running your application produces the message displaying the calculated sale price:
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.