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

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...