29

My understanding is that Parameterless constructors in structs are now allowed.

But the following gives me a compile error in VS 2015 Community

public struct Person 
{ 
    public string Name { get; } 
    public int Age { get; } 
    public Person(string name, int age) { Name = name; Age = age; } 
    public Person() : this("Jane Doe", 37) { } 
}

Error: "Structs cannot contain explicit parameterless constructors"

Anyone know why?

svick
  • 236,525
  • 50
  • 385
  • 514
myfunnyfella
  • 1,357
  • 3
  • 18
  • 25
  • This link appears to show that it should work in C# 6 with VS 2015: http://www.c-sharpcorner.com/UploadFile/0e8478/parameterless-constructors-in-structs/ Not sure why it is not working for you. – Special Sauce Jun 26 '15 at 00:51
  • Here is another article with some caveats: http://www.volatileread.com/Wiki/Index?id=1091 But nothing to explain your particular issue. Did you check to ensure your project is targeting the .NET 6.0 framework in the Project settings? – Special Sauce Jun 26 '15 at 01:00

1 Answers1

48

The feature was present in older previews of C# 6.0, which is why some articles talk about it. But it was then removed and so it's not present in the version distributed with VS 2015 RC.

Specifically, the change was reverted in pull request #1106, with more information about the rationale in issue #1029. Quoting Vladimir Sadov:

As we performed more and more testing, we kept discovering cases where parameterless struct constructors caused inconsistent behavior in libraries or even in some versions of CLR.

[…]

After reconsidering the potential issues arising from breaking long standing assumptions, we decided it was best for our users to restore the requirement on struct constructors to always have formal parameters.

The feature was then added back in C# 10.0.

svick
  • 236,525
  • 50
  • 385
  • 514
  • Interesting: although the feature was removed from the C#, it was kept in [tag:vb.net] and can be used there. OP's code sample converted to the VB (all constructors have name `New` there) will work. – miroxlav May 14 '16 at 08:53
  • @miroxlav [I don't think that's true.](http://stackoverflow.com/q/32179495/41071) – svick May 14 '16 at 10:01
  • 1
    Oh I see, although explicitly not stated, this Q&A is discussing only *non-static* ones. Static parameterless `struct` constructors work in both C# and VB. (Tested.) – miroxlav May 14 '16 at 12:02
  • Please correct me if I'm wrong. A parameterized constructor for a struct still calls default parameterless constructor (which his predefined for a struct in FCL) as first thing in its execution. I believe there is a compiler trick involved to achieve this behavior. – RBT Oct 19 '16 at 02:00
  • @RBT No, it does not. First, a struct written in C# 6.0 can't have parameterless constructor. And if you mean that parametrized constructor first initializes all fields the the default value, that's also not true, if you want that, you have to explicitly add `: this()`. – svick Oct 19 '16 at 12:41