4

The C# primary constructor feature has been 'semi' removed from C#6.

class Point(int x, int y)
{
    public int X { get; } = x; // this is supported, but now it's static only!
    public int Y { get; } = y;
}

I cannot find any indication from the team as to how likely this feature will dramatically change going forward. I am trying to find more information to make an informed decision about whether or not to take the risk of using the feature.

Resharper suppots it nicely, so it's really tempting.

Jim
  • 14,952
  • 15
  • 80
  • 167
  • 2
    I thought C# 6 already shipped without this feature. What is "'semi' removed"? – Kobi Aug 17 '15 at 12:30
  • 2
    You could follow this but its all subject to change https://github.com/dotnet/roslyn/issues/2136 – Nick Tucker Aug 17 '15 at 12:31
  • @Kobi, agreed. If I paste that exact code into VS2015 I get several syntax errors. It was removed. – dmeglio Aug 17 '15 at 12:31
  • It looks like it has been removed in the final version, I used to be able to turn it on with experimental in the configuration file, my bad... – Jim Aug 17 '15 at 12:34
  • To answer your question though, "how likely is it to change?" Well the best answer anyone can give to that is, 6 months ago it was going to be in C# 6. Today, they removed it completely (e.g. changed the feature 100%). Whose to say it will be in C# 7 at all? Perhaps it'll get canned from there too. The only people who know for sure are the language designers and they won't know until it's finalized. – dmeglio Aug 17 '15 at 12:36
  • It seems to make default properties pretty useless, because you cannot set x in public int X { get; } = x - which is supported. – Jim Aug 17 '15 at 12:36
  • @Jim default properties are implemented for supporting constants. In fact what you said is explicitly NOT supported. "Just like field initializers, auto-property initializers cannot reference ‘this’ – after all they are executed before the object is properly initialized." http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx – dmeglio Aug 17 '15 at 12:38
  • By itself, this feature didn't add enough and felt incomplete. It is really part of something bigger: record types. https://github.com/dotnet/roslyn/issues/206 Now, they can still think about how to implement it and how the syntax will be. If they kept it, it would have tied their hands. – Dennis_E Aug 17 '15 at 12:47
  • agreed, I think I have misunderstood the feature, perhaps it's not a great question... I hope I haven't wasted too much of anyone's time. – Jim Aug 17 '15 at 12:49
  • @Jim no such thing as a dumb question. I'm sure someone will benefit from this discussion. – dmeglio Aug 17 '15 at 12:52

1 Answers1

1

The C# team takes breaking changes very seriously. They only do them when the benefit is very high and the likelihood of breaking existing working code very low (for example, the change to closure behavior in foreach in C# 5 was such a breaking change).

So, if something is in a released (not preview or beta or anything like that) version of the C# compiler, you can rely on it just like on any other feature of C#. This includes getter-only auto-properties that are included in the final version of C# 6.0.

Community
  • 1
  • 1
svick
  • 236,525
  • 50
  • 385
  • 514