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

One Response

  1. Jay Van Der Zant November 24, 2014 Reply

Leave a Reply