Cloud Training (Online)




Lambda and Getter Only Auto-Properties in C# 6.0

C# 6.0 Features Series

In C# 6.0 , you can use the lambda expressions to implement the logic for the Getter Only Auto-Properties . This brings the usage of the lambda expression to the getter only auto properties in C# 6.0.

Lambda and Getter Only Auto-Properties in C# 6.0

When the lambda expression is encountered , it indicates to the compiler that this is a getter only property and not a field.

using System;
namespace MobileOSApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp = new Employee { FirstName = "Senthil", LastName = "Kumar" };
            Console.WriteLine(emp.LastName);
            Console.ReadLine();
        }
    }
    public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        // Lambda expression and Getter Only auto property.
        public string FullName => (FirstName + " " +LastName);
    }
}

image

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.