#1,216 – C# 6.0 – Initializing Read-Only Auto-Properties from Constructors

In C# 6.0, when defining a read-only auto-property you can initialize the property as part of its declaration.

  public DateTime DogCreationTime { get; } = DateTime.Now;

You can also initialize these read-only auto-properties from a constructor.  For example:

    public class Dog
    {
        public string Name { get; }

        public Dog(string name)
        {
            Name = name;
        }
    }
Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #1,216 – C# 6.0 – Initializing Read-Only Auto-Properties from Constructors

  1. Pingback: Dew Drop – October 31, 2014 (#1889) | Morning Dew

  2. Pingback: Parsing TypeScript definitions (functional-ly.. ish) | Dinesh Ram Kali.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: