I have an abstract class as a base class for various implementations.
Let it look like this:
public abstract class MyBase {
public object Property1 { get; protected set; }
}
It is important, that the type of Property1 is object and not object?.
I want it to be completely clear that the Property1 is never null in implementation instance.
Let's say a typical implementation looks like this:
public sealed class MyImpl : MyBase {
public MyImpl() {
Property1 = new object();
}
}
Of course in real world there are plenty of properties in the base class and they are of various types, not just object. I could mark the properties as abstract, but then I would have to implement them in each implementing class - that would mean more boilerplate code with no real benefit in that, right?
OK, but now I get a warning, that the properties must have non-null values before exiting constructor - CS8618. Of course I can disable the warning for my base class, but is there a more elegant way to instruct the compiler that the implementing class MUST set those properties to non-null values instead of disabling the warning?
It would be perfect if I get the warning not for the base class, but for the implementation if it doesn't set one of those properties.
As I already figured out - the latest C# compiler and Visual Studio has plenty of magic special attributes that change the nullable warning behavior, but I don't know where to look.