HomeCSharpLambda and Getter Only Auto-Properties in C# 6.0

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...