For example:
public class Stereo {
public Speaker? LeftSpeaker;
public int? Volume;
}
The int? works fine, but I'm unable to make Speaker nullable. This is turning out to be surprisingly hard to Google, I thought this would be simple ...
Why would you want that? Non-value types (i.e. reference types) can already be set and compared to null.
Adding a Nullable<T> wrapper over reference-type objects would create two layers of nullability; pointless complexity, hence it is not allowed.
duh.Wild swag:
public class Stereo {
public Speaker LeftSpeaker;
public int? Volume;
public Stereo() {
LeftSpeaker = null; // Mission accomplished.
}
}
() didn't I?A lot of fish already here - but if you want to learn how to fish, I suggest having a look into Jon Skeet's book C# in Depth. Chapters 4.1 to 4.5 are dealing with all facets of the "concept of nullity" in C#.