I'm doing a simulation for Microsoft's C # certification and I came across the following 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:
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: