Book Image

C# Programming Cookbook

By : Dirk Strauss
Book Image

C# Programming Cookbook

By: Dirk Strauss

Overview of this book

During your application development workflow, there is always a moment when you need to get out of a tight spot. Through a recipe-based approach, this book will help you overcome common programming problems and get your applications ready to face the modern world. We start with C# 6, giving you hands-on experience with the new language features. Next, we work through the tasks that you perform on a daily basis such as working with strings, generics, and lots more. Gradually, we move on to more advanced topics such as the concept of object-oriented programming, asynchronous programming, reactive extensions, and code contracts. You will learn responsive high performance programming in C# and how to create applications with Azure. Next, we will review the choices available when choosing a source control solution. At the end of the book, we will show you how to create secure and robust code, and will help you ramp up your skills when using the new version of C# 6 and Visual Studio
Table of Contents (21 chapters)
C# Programming Cookbook
Credits
About the Author
Acknowledgements
About the Reviewer
www.PacktPub.com
Preface
Index

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.