Auto-Property Initializers
In C#6.0, you can initilizes the property inline instead initilizing in type constructor.
See the below example.
Before C#6
using System;
namespace OLDCSharp
{
class Program
{
static void Main(string[] args)
{
EmployeeDetails employee = new EmployeeDetails();
Console.WriteLine("Name: " +
employee.Name + "\nSalary: " + employee.Salary);
Console.ReadKey();
}
public class EmployeeDetails
{
public string Name { get; set; }
public decimal Salary { get; set; }
public EmployeeDetails()
{
/* Initializing property through constructor */
Name = "Peter Hansen";
Salary = 20000;
}
}
}
}
In C#6.0
using static System.Console;
namespace NewInCSharp6
{
class Program
{
static void Main(string[] args)
{
EmployeeDetails employee = new EmployeeDetails();
WriteLine("Name: " +
employee.Name + "\nSalary: " + employee.Salary);
ReadKey();
}
public class EmployeeDetails
{
/* New way to initilize*/
/* Getter only property with inline initialization */
public string Name { get; } = "Peter Hansen";
/* Property with inline initialization */
public decimal Salary { get; set; } = 20000;
}
}
}
Name: 'nameof' expression
C# 6.0 Feature : Auto-Property Initializers
Reviewed by kamal kumar das
on
November 29, 2016
Rating:

No comments: