For an assignment I have to write a Tribool class in C# using a struct. There are only three possible tribools, True, False, and Unknown, and I have these declared as static readonly. Like this:
public static readonly Tribool True, False, Unknown;
I need my default constructor to provide a False Tribool, but I’m not sure how to go about this. I’ve tried Tribool() { this = False; } and Tribool() { False; } but I keep getting a “Structs cannot contain explicit parameterless constructors” error.
The assignment specified that the default constructor for Tribool should provide a False Tribool. Otherwise, a user should not be able to create any other Tribools. I don’t really know what to do at this point. Any advice would be greatly appreciated. Thanks.
As the error is telling you, you absolutely can not have a parameterless instance constructor for a struct. See §11.3.8 of the C# 3.0 language specification:
The language provides one for you known as the default constructor. This constructor returns the value of the struct where are fields have been set to their default value.
Now, what you could do is have the default value of the struct represent the
Falsevalue. I’ll leave the details to you.