I would like the structure to always be valid with respect to a specific contract executed by the constructor. However, the contract is violated by the default operator.
Consider the following, for example:
struct NonNullInteger { private readonly int _value; public int Value { get { return _value; } } public NonNullInteger(int value) { if (value == 0) { throw new ArgumentOutOfRangeException("value"); } _value = value; } }
As a workaround, I changed my structure to a class, so I can guarantee that the constructor is always called when the new instance is initialized. But I wonder if there is absolutely no way to get the same behavior with structure?
source share