Professional Documents
Culture Documents
In Focus
C#IsCorner
Look Who Speaking at #CS
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 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.
Code
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. }
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
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
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
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.
Rizwan Ali
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
OUR TRAINING
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
TRENDING UP
01 C# Coding Standards 😎
Implement CRUD Operations With Sorting, Searching And Paging Using EF Core In ASP.NET
06 Core
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