Parameterless constructors in structs for C# 6

后端 未结 2 1079
孤街浪徒
孤街浪徒 2020-12-10 01:02

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

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

publi         


        
相关标签:
2条回答
  • 2020-12-10 01:12

    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.

    0 讨论(0)
  • 2020-12-10 01:29

    I'm not sure why, however, this is allowed:

    public struct Person 
    { 
        public string Name { get; } 
        public int Age { get; } 
        public Person(string name = null, int age = 0) { Name = name; Age = age; } 
    }
    

    Does that solve your problem?

    0 讨论(0)
提交回复
热议问题