#1,210 – C# 6.0 – Primary Constructors

NOTE: Primary constructors will not be shipping in C# 6.0, after all.  They were implemented in the language, but did not yet have appropriate downstream work done in time for the release of 6.0 (i.e. in Visual Studio and debugger).  Thanks to Steve Hall for pointing this out.  –SPS, 23Oct14

C# 6.0 introduces the notion of a primary constructor–a constructor defined as part of the type declaration, rather than as a separate method.

Consider the C# 5.0 example below, with a traditional constructor that initializes a couple of properties.  (Note that Name property is not immutable, but only protected).

    public class Dog
    {
        public Dog(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public string Name { get; protected set; }
        public int Age { get; set; }
    }

The primary constructor syntax allows us to simplify this code, moving the constructor into the class declaration.  We can then use auto-property initializers that reference the constructor’s parameters.

    public class Dog(string name, int age)
    {
        public string Name { get; protected set; } = name;
        public int Age { get; set; } = age;
    }

This second code fragment behaves exactly like the first.

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,210 – C# 6.0 – Primary Constructors

  1. Steve Hall says:

    Unfortunately, primary constructors have been cut from Roslyn, per the announcement at CodePlex: https://roslyn.codeplex.com/discussions/568820

    Keep an eye on the language status feature table at https://roslyn.codeplex.com/wikipage?title=Language%20Feature%20Status before posting any more C# 6.0 tips.

    BTW, your “2,000 things” blogs for C# and WPF are great! I’m continuously re-learning details I haven’t needed recently… I’m hoping you’ll collect them all into one or more books someday. (Hint….hint…..)

    • Sean says:

      Thanks Steve, I hadn’t seen the announcement on the Roslyn site at Codeplex. I’d been working off Michaelis’ MSDN article summarizing the features and hadn’t seen Mads’ comments on primary constructors.

      What’s interesting is that I got the impression from Mads that primary constructors are out of this release only because the tooling wasn’t ready, not because it wasn’t a good feature. My assumption is that it will show up at some point in the future. However, the commenters on Codeplex make it sound like it was removed because it wasn’t a useful feature.

      Thanks also for the link to the feature table. That’s helpful.

      I’ll leave this post in place, but revise to make it clear that the feature didn’t make it into 6.0.

      Thanks again,
      Sean

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: