I am exploring new features introduced in C#. In last post, I looked at Auto-Property enhancements in C# . Now I am exploring “Expression-bodied function members” feature.
Lets get our hand dirty with this new feature. Many times we have methods whose body consist on single line of code like WriteLog, Add two number etc.
Lets get our hand dirty with this new feature. Many times we have methods whose body consist on single line of code like WriteLog, Add two number etc.
1
2
3
4
5
6
7
8
9
| public int Add ( int first, int second) { return first + second; } public void WriteLog() { Console.WriteLine(Message); } |
Expression-bodied function members allow properties, methods and other function members to have bodies as lambda like expressions instead of statement blocks. Thus reducing lines of codes and clear view of the expressions.It works for methods and read-only properties.
1
2
3
| public string Message { get ; set ; } = "Message" ; public int AddEx( int first, int second) =>; first + second; public void WriteLogEx() =>; Console.WriteLine($ "{Message}" ); |
No comments:
Post a Comment