Expression Bodied Functions and Properties in C# 6.0

Introduction

In November 2014 Microsoft introduced the latest version of the Visual Studio. This latest version is known as the Visual Studio 2015 preview. In this new version of Visual Studio Microsoft introduced many new features and enhancements of C# 6.0. In C# 6.0 Microsoft added many new features that are very useful for developers and that make programming easier for developers. One of the features is Expression Bodied Functions and Properties that is very useful feature for C# 6.0 programmers. C# 6.0 is the latest version of the C# language that makes some new changes in it. It includes many new features in it. I will explain for you the Expression Bodied Functions and Properties feature.

Expression Bodied Functions and Properties

Expression Bodied Functions and Properties is the latest feature of C# 6.0. The main advantage is the use of expression bodies to reduce the code. Using expression bodies you can create the expression bodies for the function/method, property and reduce the code. If you are familiar with lambda expressions then expression bodies are similar to lambda expressions in C#. As we know lambda expressions use functions and are called by a delegate and Expression bodies are similar to lambda expression functions.

Before getting to body expression methods in C# 6.0 you must write a method many times for some lines of code, but now using expression body members you can do that easier and in a single line of code. Using expression bodies you reduce the code and here you do not want the use of curly braces. In simple words I want to say that the code that you have written in the { } (curly brasses) has been altered with the =>, please read to see the example. Using this example you can easily understand the scenario. But here one thing to remember is that here => does not work as a lambda operator so here the compiler does not covert this into a delegate type. It is just like a formatting pattern to reduce the code.



I tell you how to convert a property, an operator and a method with the expression bodies.

Nothing new in the beginning. Just open your Visual Studio 2015, create a new project, select the language C#, select the console application and write the code depending on your needs.

Example Property

In this example we just only create a property in the Visual Studio 2015 using C# code and tells you how to reduce the code of the statement using the expression bodies member method.

This code is simple. It declares a property and gets its value.

Code

  1. using System;  
  2.   
  3. public class user  
  4. {  
  5.     private string name = "rizwan";  
  6.   
  7.     public string name1  
  8.     {  
  9.         get { return name; }  
  10.     }  
  11. }  
  12.   
  13. public class ReadonlyProperties  
  14. {  
  15.     static void Main()  
  16.     {  
  17.         user p = new user();  
  18.   
  19.   
  20.         Console.WriteLine(p.name1);  
  21.         Console.Read();  
  22.     }  
  23. }  
Now we make the expression bodies member of this property.
  1. using System;  
  2.   
  3. public class user  
  4. {  
  5.     private string name = "rizwan";  
  6.   
  7.     public string name1=>name;  
  8.      
  9. }  
  10.   
  11. public class ReadonlyProperties  
  12. {  
  13.     static void Main()  
  14.     {  
  15.         user p = new user();  
  16.        
  17.   
  18.         Console.WriteLine(p.name1);  
  19.         Console.Read();  
  20.     }  
  21. }  
Output: Both provide the same output.



Example Operator

In this program we discuss the operator. Here we discuss how the operator works in a simple C# application and how to work with Expression bodies when we create Expression bodies for it.

Code
  1. using System;  
  2. namespace operators  
  3. {  
  4.     class operatorwork  
  5.     {   
  6.         int length = 5;  
  7.         int width = 10;  
  8.   
  9.         public int area()  
  10.         {  
  11.             return length * width;  
  12.         }  
  13.         public void showarea()  
  14.         {  
  15.   
  16.             Console.WriteLine("area of reactangle: {0}", area());  
  17.         }  
  18.     }  
  19.   
  20.     class rectangle  
  21.     {  
  22.         static void Main(string[] args)  
  23.         {  
  24.            operatorwork obj = new operatorwork();  
  25.   
  26.             obj.showarea();  
  27.             Console.ReadLine();  
  28.             Console.Read();  
  29.         }  
  30.     }  
  31. }  
Now we make the Expression bodies for it. So here we can check how to work with it.

Code
  1. using System;  
  2. namespace operators  
  3. {  
  4.     class operatorwork  
  5.     {  
  6.         int length = 5;  
  7.         int width = 10;  
  8.   
  9.         public int area()=> length * width;  
  10.          
  11.         public void showarea() => Console.WriteLine("area of reactangle: {0}", area());  
  12.         
  13.     }  
  14.   
  15.     class rectangle  
  16.     {  
  17.         static void Main(string[] args)  
  18.         {  
  19.             operatorwork obj = new operatorwork();  
  20.   
  21.             obj.showarea();  
  22.             Console.ReadLine();  
  23.             Console.Read();  
  24.         }  
  25.     }  
  26. }  
Output
 



Example Method

In this example we create two methods one is for addition and the other is for subtraction. In the first we create a simple program and after that we tell you how to reduce the method statement code using the expression bodies method.

Code
  1. using System;  
  2.   
  3. namespace ConsoleApplication1  
  4. {  
  5.     public class sum  
  6.     {  
  7.         public int add(int x, int y)  
  8.         {  
  9.             return x + y;  
  10.         }  
  11.   
  12.         public int sub(int x, int y)  
  13.         {  
  14.             return x - y;  
  15.         }  
  16.     }  
  17.   
  18.     public class main  
  19.     {  
  20.         static void Main()  
  21.         {  
  22.             sum obj = new sum();  
  23.             int sum = obj.add(10, 20);  
  24.             int sub = obj.sub(30, 10);  
  25.   
  26.             Console.WriteLine("The Sum of two numbers"+ sum);  
  27.             Console.WriteLine("The substraction of two numbers" + sub);  
  28.             Console.Read();  
  29.         }  
  30.   
  31.     }  
  32. }  
Now we use the expression bodies member to reduce this code and make this code into one line of code. Refer to the example.

Code

  1. using System;  
  2.   
  3.   
  4. namespace ConsoleApplication2  
  5. {  
  6.     public class sum  
  7.     {  
  8.         public int add(int x, int y) => x + y;  
  9.          
  10.   
  11.         public int sub(int x, int y) => x - y;  
  12.          
  13.     }  
  14.   
  15.     public class main  
  16.     {  
  17.         static void Main()  
  18.         {  
  19.             sum obj = new sum();  
  20.             int sum = obj.add(10, 20);  
  21.             int sub = obj.sub(30, 10);  
  22.   
  23.             Console.WriteLine(sum);  
  24.             Console.WriteLine(sub);  
  25.   
  26.   
  27.             Console.Read();  
  28.         }  
  29.   
  30.     }  
  31.   
  32. }  
Output



Please be careful. Expression bodies work with only a single return statement, in other words it will work only a single return statement block.

Summary

In this article we read about Expression Bodied Functions and Properties in C# 6.0. As I said above, it works like lambda expressions but does not create the function for the delegates as is usually created in the condition of the lambda expression. We all know that this is a new feature of C# 6.0, so we cannot say that it is much and more beneficial for the users but one thing is clear that it will provide us a statement less code that is neat and clean code. It will also reduce the length of the code. Sometimes we miss the brackets in our program and get some errors during the compilation so it also overcomes this problem. I hope you all enjoyed this article. 

Next Recommended Readings