Initialize Read-only Properties from Constructor in C# 6.0

Published on January 3, 2015 by abundantcode

Filed under ADO.NET

Last modified January 3, 2015

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 7936 times

C# 6.0 lets the developers to initialize the read only properties directly when declaring them .

Since it is a read-only property , it behaves the same as read only variable where you can initialize the read-only auto properties from a constructor.

Initialize Read-only Properties from Constructor in C# 6.0

Below is a sample code snippet demonstrating how to initialize read-only properties from constructor in C# 6.0

public class Employee
{
        public string ACName { get; set; }
        public DateTime CreatedDate { get;  }
        public Employee()
        {
            CreatedDate = DateTime.Now;
        }
}

Leave a Comment


x

You might be Interested in these Posts

3 New C# 8 Features We Are Excited About

C# is rapidly approaching its third decade of life. Originally released in 2000, the language has gr...

Quick Tip —CSS Containment Module by @gilfink

One of the new hidden gems in the new CSS modules, which is supposed to drop in new browser versions...

C Program to a print pascal triangle

Problem Statement Write a program in C to print pascal triangle. How to print a pascal triange in ...