4

Consider the following structure in C#:

public struct TestStruct
{
    public int Number { get; set; }

    public TestStruct(int num)
    {
        Number = num;
    }
}

I'm very familiar with the compilation error which occurs if one tries to compile this (this and that questions provide an example).

However, I've recently noticed that such structure is compiled perfectly in Visual Studio 2015.

I wasn't able to find any change log for the compiler which would include the aforementioned behaviour. Could anyone provide any guidance as to where to find such information? I've found the mentions of something similar here.

Furthermore, the page about the compiler error CS0188 states:

Auto-implemented properties should be avoided in structs because they have no backing field and therefore cannot be initialized in any way from the constructor.

However, I was not able to observe the inability to initialise the property from the constructor.

Community
  • 1
  • 1
Vlad Stryapko
  • 1,035
  • 11
  • 29

1 Answers1

4

This is new in C#6 (which is what you'll be using by default in VS2015). The description for that compiler error isn't strictly correct, either. Auto-properties have always had a backing field, it's just generated by the compiler and not otherwise directly accessible from the code. So it's a bit misleading.

The changes to auto-property initialization in C# now allow the compiler to generate code to set the backing field rather than attempting to call the setter method in the constructor. This also makes it much easier to create immutable structs:

public struct Foo
{
    public string ReadOnlyString { get; }

    public Foo( string prop )
    {
        ReadOnlyString = prop;
    }
}
Kyle
  • 6,500
  • 2
  • 31
  • 41
  • I've forgotten to mention that I've tried setting different language versions for the project and it works perfectly even with the C# 4. The example you provided doesn't work with C# 4, as expected. It seems to me it's some compiler issue rather than the language one. Anyway, it seems logical but I don't get why it wasn't mentioned anywhere and wasn't provided in the section describing property initialisation for C# 6. – Vlad Stryapko Feb 01 '17 at 14:57
  • 1
    @vlad.stryapko Changing the language version doesn't quite do what you think it does. Setting different compiler versions only changes what syntax the compiler will accept. It doesn't change how the compiler actually works. See [this question](http://stackoverflow.com/questions/29476057/how-to-compile-c-sharp-with-specific-language-version). – Kyle Feb 01 '17 at 15:43
  • So, have I got it right that in order to support read-only properties in C# 6, the changes were made to the compiler which made it work differently and set the field directly, therefore not raising the error? – Vlad Stryapko Feb 01 '17 at 15:55
  • @vlad.stryapko For the most part. The changes also apply to properties that aren't read-only. The way properties are initialized was just changed in general. – Kyle Feb 01 '17 at 16:57