Over on StackOverflow, saw an interesting question that taught me something new about C# 3.0.
The question Automatic Properties and Structures Don’t Mix? asked why this code doesn’t work:
Turns out that the solution is to properly chain the declared constructor with the default parameterless constructor:
Which just goes to show that taking shortcuts (like not properly chaining constructors) can turn around and bite.
Interestingly, though, the C# compiler gives you all the information you need to solve this as a part of the error messages (emphasis added):
error CS0188: The ‘this’ object cannot be used before all of its fields are assigned to
error CS0843: Backing field for automatically implemented property ‘WindowsApplicationSample.MyStruct.Size’ must be fully assigned before control is returned to the caller. Consider calling the default constructor from a constructor initializer.
Someone posted another solution, taken from a Codeplex project - though in this case I think the “solution” is a problem in it’s own right … check it out.
Look at the “Kung fu!” like - assignment to this - Yikes!