98

Until this very day, I could make use of primary constructors, such as:

public class Test(string text)
{
    private string mText = text;
}

To be able to do this, in the previous Visual Studio CTP, I had to add this to the csproj-file:

<LangVersion>Experimental</LangVersion>

Anyhow, this no longer works in the Visual Studio 2015 Preview (with or without LangVersion). Does anyone have any ideas about what could be going on?

Mafii
  • 7,227
  • 1
  • 35
  • 55
Matko
  • 991
  • 1
  • 6
  • 5
  • 6
    They talk about removing it [here](https://roslyn.codeplex.com/discussions/568820) – Jonesopolis Nov 13 '14 at 18:22
  • 15
    Am I the only one that thought this feature was pointless? I never found the actual benefit for it. – TyCobb Nov 13 '14 at 18:26
  • 2
    It felt a little bit awkward, but I like the philosophy behind it! – Matko Nov 13 '14 at 18:35
  • 14
    That's a shame. But you can use primary constructors freely once you move to F#. Move to F#, csharpers, your new language features are already there ;) – scrwtp Nov 13 '14 at 19:11
  • 3
    @TyCobb, primary constructors are absolutely essential with dependency injection: you can use constructor injection (as opposed to property injection) without repeating the name (and type) of every dependency four times. – Fyodor Soikin Jul 31 '15 at 23:25
  • 1
    @TyCobb primary constructors would make creating immutable classes less painful, see [this proposal](http://roslyn.codeplex.com/discussions/543522) for what we could have had. With immutable objects you can share data easily across thread borders as they don't need to be syncronized. Writing such classes means writing much boilerplate code. Read-only properties introduced C#6 are a step in the right direction though. – mbx Oct 23 '15 at 17:45
  • Today, there is a proposal added here: https://github.com/dotnet/csharplang/blob/27487fa/proposals/primary-constructors.md. – vulcan raven Jul 24 '19 at 13:26

1 Answers1

144

Does anyone have any ideas about what could be going on?

Yup - primary constructors have been removed from the plans for C# 6. They may well make an appearance in some form in a later version, but they're not in C# 6 any more.

See the "Changes to the language feature set" post from the team for more details.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • That's really sad! They seemed quite useful. – Per Lundberg Jan 04 '18 at 13:11
  • @PerLundberg: They're not in C# 7 either. I suspect the C# design team will revisit record types in C# 8, but that doesn't mean they'll definitely make it there. – Jon Skeet Jan 04 '18 at 13:33
  • Let's hope for the best. :) I saw them mentioned in some C# posting yes, and it looked like another interesting way to try and solve the "simple class" problem. – Per Lundberg Jan 05 '18 at 21:15