Cloud Training (Online)




Lambda Expression for Function Members in C# 6.0

C# 6.0 Features Series

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();
        }
    }

Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.