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

NOTE: The following is proposed syntax for Roslyn, but not yet working in the Visual Studio 2014 CTP, as of 30 Oct 2014.  The Codeplex site lists this as a “Done” feature for Roslyn, but it’s not absolutely clear whether this will ship with Visual Studio 2014.

In C# 6.0, you’ll be able to define read-only auto-properties you can initialize the property as part of its declaration.

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

The intent in C# 6.0 is that you’ll also be able to initialize these read-only auto-properties from a constructor.  For example:

    public class Dog
    {
        public string Name { get; }

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

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: