Subscribe to our YouTube Channel for more Videos

Expression Bodies on Methods returning void in C# 6.0


C# 6.0 Features Series

In one of the previous blog post , we explored one of the feature of C# called Lambda Expression for Function Members in C# 6.0 where the example method used there had a return type of integer. In this blog post , let see the usage of the expression bodies on the methods returning void.

Expression Bodies on Methods returning void in C# 6.0

The Expression bodies can still be used on the methods returning void or Task (async methods) . In this case , it is necessary that expression immediately after the lambda expression syntax (arrow) to be a statement expression.

Below is a sample code snippet demonstrating the usage of the Expression Bodies on Methods returning void in C# 6.0

using System;
using System.Collections.Generic;
using System.Linq;

namespace MobileOSGeekApp
{
    class Program
    {
        public static void Display() => Console.WriteLine("Welcome to developerpublish.com .NET Tutorials Section");
        static void Main(string[] args)
        {
            Display();

            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.