You are on page 1of 10

4/10/2019 Expression Bodied Functions and Properties in C# 6.

In Focus
C#IsCorner  
Look Who Speaking at #CS

ASK A QUESTION CONTRIBUTE

Expression Bodied Functions and Properties in C#


6.0
In this article, I will provide information about Expression Bodied Functions and Properties
including how they work.

Rizwan Ali Jan 05 2015

0 1 13.5k

expressionbodiedmember.rar
Download Free .NET & JAVA Files API

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.

https://www.c-sharpcorner.com/UploadFile/66489a/expression-bodied-members-in-C-Sharp-6-0/ 1/10
4/10/2019 Expression Bodied Functions and Properties in C# 6.0

InBefore
Focus getting to body expression methods in C# 6.0 you must write a method many times for 
C#IsCorner
Look Who Speaking at #CS
some lines of code, but now using expression body members you can do that easier and in a
ASK Athe
single line of code. Using expression bodies you reduce QUESTION
code and here youCONTRIBUTE
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

01. using System;


02.
03. public class user
04. {
05. private string name = "rizwan";
06.
07. public string name1
08. {
09. get { return name; }
10. }
11. }
https://www.c-sharpcorner.com/UploadFile/66489a/expression-bodied-members-in-C-Sharp-6-0/ 2/10
4/10/2019 Expression Bodied Functions and Properties in C# 6.0

12.
In Focus  
13.
Look C#public
Who IsCorner class
Speaking ReadonlyProperties
at #CS
14. {
15. static void Main() ASK A QUESTION CONTRIBUTE
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.

01. using System;


02.
03. public class user
04. {
05. private string name = "rizwan";
06.
07. public string name1=>name;
08.
09. }
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
https://www.c-sharpcorner.com/UploadFile/66489a/expression-bodied-members-in-C-Sharp-6-0/ 3/10
4/10/2019 Expression Bodied Functions and Properties in C# 6.0

In Focus
01. using System;
C#namespace  
02.
Look Who IsCorner
Speaking atoperators
#CS
03. {
04. class operatorwork ASK A QUESTION CONTRIBUTE
05. {
06. int length = 5;
07. int width = 10;
08.
09. 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

01. using System;


02. namespace operators
03. {
04. class operatorwork
05. {
06. int length = 5;
07. int width = 10;
08.
09. public int area()=> length * width;
10.
11. public void showarea() => Console.WriteLine("area of reactangle: {
12.
13. }
14.
15. class rectangle
16. {
17. static void Main(string[] args)
https://www.c-sharpcorner.com/UploadFile/66489a/expression-bodied-members-in-C-Sharp-6-0/ 4/10
4/10/2019 Expression Bodied Functions and Properties in C# 6.0

18.
In Focus {  
19.
Look C# Corner operatorwork obj = new operatorwork();
Who Is Speaking at #CS
20.
21. obj.showarea(); ASK A QUESTION CONTRIBUTE
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 rst we create a simple program and after that we tell you how to reduce the method
statement code using the expression bodies method.

Code

01. using System;


02.
03. namespace ConsoleApplication1
04. {
05. public class sum
06. {
07. public int add(int x, int y)
08. {
09. 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.
https://www.c-sharpcorner.com/UploadFile/66489a/expression-bodied-members-in-C-Sharp-6-0/ 5/10
4/10/2019 Expression Bodied Functions and Properties in C# 6.0

26.
In Focus Console.WriteLine("The Sum of two numbers"+ sum);  
27.
Look C# Corner Console.WriteLine("The substraction of two numbers" + sub);
Who Is Speaking at #CS
28. Console.Read();
29. } ASK A QUESTION CONTRIBUTE
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

01. using System;


02.
03.
04. namespace ConsoleApplication2
05. {
06. public class sum
07. {
08. public int add(int x, int y) => x + y;
09.
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

https://www.c-sharpcorner.com/UploadFile/66489a/expression-bodied-members-in-C-Sharp-6-0/ 6/10
4/10/2019 Expression Bodied Functions and Properties in C# 6.0

In Focus
C#IsCorner  
Look Who
Please be Speaking at #CS
careful. Expression bodies work with only a single return statement, in other words it
will work only a single return statement block. ASK A QUESTION CONTRIBUTE

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 bene cial 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. 

C# 6.0 Expression Bodied Members xpression bodies

Rizwan Ali

I have done my Master's in Computer Science from Teerthanker Mahaveer University.


Currently, I work with ASP.NET technology.
https://www.c-sharpcorner.com/members/rizwan-ali13

312 1.8m

0 1

Type your comment here and press Enter Key (Minimum 10 characters)

nice...

https://www.c-sharpcorner.com/UploadFile/66489a/expression-bodied-members-in-C-Sharp-6-0/ 7/10
4/10/2019 Expression Bodied Functions and Properties in C# 6.0

In Focus K P Singh Chundawat Jan 06, 2015


C#IsCorner  
Look Who Speaking at #CS
922 1k 131.5k 0 0 Reply

ASK A QUESTION CONTRIBUTE

OUR TRAINING

Mastering React and TypeScript


Learn React and TypeScript Step by Step.

https://www.c-sharpcorner.com/UploadFile/66489a/expression-bodied-members-in-C-Sharp-6-0/ 8/10
4/10/2019 Expression Bodied Functions and Properties in C# 6.0

In Focus
C#IsCorner  
Look Who Speaking at #CS

ASK A QUESTION CONTRIBUTE

TRENDING UP

01 C# Coding Standards 😎

02 Top 10 JavaScript Frameworks In The World

03 Angular 7 Routing And Preserving Trailing Slash In URL⚔

04 Microservices Using ASP.NET Core

05 Most Popular Front End JavaScript Framework In The World

Implement CRUD Operations With Sorting, Searching And Paging Using EF Core In ASP.NET
06 Core

07 Building High Performance Back End (SQL Server)

08 Upload Files In Azure Blob Storage Using ASP.NET Core

ASP.NET MVC Request Life Cycle


https://www.c-sharpcorner.com/UploadFile/66489a/expression-bodied-members-in-C-Sharp-6-0/ 9/10
4/10/2019 Expression Bodied Functions and Properties in C# 6.0

In Focus
09 C#IsCorner
Look Who Speaking at #CS
 

10 Filtration, Sorting, And Pagination In Angular 7 Using Web API And SQL Server
ASK A QUESTION CONTRIBUTE
View All

About Us Contact Us Privacy Policy Terms Media Kit Sitemap Report a Bug FAQ Partners
C# Tutorials
©2019 C# Corner. All contents are copyright of their authors.

https://www.c-sharpcorner.com/UploadFile/66489a/expression-bodied-members-in-C-Sharp-6-0/ 10/10

You might also like