Initializers for Read-Only Auto Properties in C# 6.0

C# 6.0 Features Series

In C# 6.0 , we can use the initializers for the auto properties to initialize the value for the read only auto properties .

How to Initialize a Read-Only Auto Properties in C# 6.0 ?

Here’s a code snippet that demonstrates how to initialize a read-only auto property in C# 6.0.

public class Employee
{
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime ModifiedDate { get;  } = DateTime.Now;
}

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...