C# 6.0 Features Series
- How to try C# 6.0 and Rosyln?
- Getter-only (Read Only) Auto Properties in C# 6.0
- Lambda and Getter Only Auto-Properties in C# 6.0
- Initializers for Read-Only Auto Properties in C# 6.0
- Initializers via Expression Auto Properties in C# 6.0
- C# 6.0 – A field initializer cannot reference the non-static field, method, or property
- Lambda Expression for Function Members in C# 6.0
- Dictionary Initializers (Index Initializers) in C# 6.0
- Expression Bodies on Methods returning void in C# 6.0
- using keyword for static class in C# 6.0
- Unused namespaces in Different Color in Visual Studio 2015
- Null-Conditional Operator in C# 6.0
- Null-Conditional Operator and Delegates
- nameof Operator in C# 6.0
- Contextual Keywords in C#
- String Interpolation in C# 6.0
- Exception Filters in C# 6.0
- Await in Catch and finally block in C# 6.0
Having the lambda expression for the function members is one of the cool feature specially for simple and straight forward methods.
In the earlier versions of C# , lambda expression was one of the option when an delegate was expected as input .
For example ,
using System; using System.Linq; namespace MobileOSGeekApp { class Program { static void Main(string[] args) { Func<int, int,int> AddNumber = (a, b) => a + b; Console.WriteLine(AddNumber(1,2)); Console.ReadLine(); } } }
C# 6.0 allows the developers to use lambda expression as the body of the function member.
Lambda Expression for Function Members in C# 6.0
Code snippet demonstrating the usage of the Lambda expression for the function members in C# 6.0
using System; using System.Linq; namespace MobileOSGeekApp { class Program { // Lambda expression used in function member public static int AddNumber(int a, int b) => a + b; static void Main(string[] args) { Console.WriteLine(AddNumber(1,2)); Console.ReadLine(); } }