1

I'm doing a simulation for Microsoft's C # certification and I came across the following question:

Question In the image below we can see that I chose the third option, however, according to the simulation, the correct answer would be the last one:

answers

I understand that it is a good practice not to expose members of a struct type beyond the constructor, so I chose the option where setters are private and set through the constructor.

I did a struct and had no compilation or execution errors for using a private set property:

class Program
{
    static void Main(string[] args)
    {
        var test = new TestClass(1);
    }

}

struct TestClass
{
    public TestClass(int value)
    {
        this.MyProperty = value;
    }
    public int MyProperty { get; private set; }
}

The explanation of the exercise follows, I honestly did not understand: explanation

  • See the last sentence of paragraph 2, "Because the compiler does not require the use of any constructors when using structs...." . Because compiler compiler might NOT initialized a struct in constructor, A private setter means compiler can't initialize the value. – Louis Go Mar 10 '20 at 05:32
  • Thank you, it make sense , in case that I do not have a constructor I will never be able to initialize a private property. But the example give to me a constructor so the properties will be initialized through it. No? – Rafael Pimenta Mar 10 '20 at 05:54
  • You can't assure user always using a ctor with parameters. One could always call `new Point3d()`. – Louis Go Mar 10 '20 at 06:03

1 Answers1

3

Unfortunately, that question is out of date.

In earlier versions of C#, it was indeed a problem to declare properties like that in a struct. You're not allowed to access the properties before the struct has been initialized, but you can't initialize the backing field without accessing the property. It's a Catch 22. In those versions, as the explanatory text the test presented you says, the way around this issue was to include : this() in your constructor declaration, so that the initialization rules are followed.

However, as your own test demonstrates, this is no longer the case. C# has loosened the struct initialization rules to allow initialization for auto-implemented properties in a struct.

It's an unfortunate question. Frankly, even if the C# struct initialization rules hadn't changed, the question itself promotes the poor practice of a mutable struct, as well as of exposing fields as public. It's highly debatable, in spite of the presence of examples just like that in Microsoft-published code, whether it's really all that much of a hardship to invoke the parameterless constructor so that you can use auto-implemented properties. And even if one decides it is, and thus chooses to make the members fields instead, it's also debatable whether those fields should be writeable instead of readonly.

(The usual argument goes along the lines of "well, this data structure needs to be mutable so that values can be updated more efficiently, or used in interop, or whatever", and certainly in some cases those arguments are valid. But I don't think that necessarily supports the idea of assuming those special cases for the purposes of the test question.)

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136