The .NET Framework 3.0 introduced the auto-implemented properties which was quite useful but it had one drawback . This did not have an option to specify the default value to the property .
Here’s an example of the auto-implemented property in C#.
public class Employee
{
public string Name { get; set; }
}If you had to specify the default value to property , you must either initialize the value in the constructor or by specifying the explicit backing field.
public class Employee
{
public Employee()
{
Name = "Abundantcode";
}
public string Name { get; set; }
}Auto Property Initializers in C# 6.0
If you want to specify the default value for the property , you can use the auto property initializers in C# 6.0 to do it.
Below is a code snippet demonstrating how to initialize a property with a value with the new auto-property initializer feature in C# 6.0.
public class Employee
{
public string Name { get; set; } = "Abundantcode";
}
Leave a Reply