Why is my constructor telling me that a field must contain a non-null value when I am assigning it in the constructor but through a property?
In my class, I set the value of _owner
indirectly in the constructor through the Owner
property. However, the compiler still thinks that a value will not be assigned by the end of the constructor. The property that I used to assign the backing field already does the null check. Why is this warning happening, and how can I fix it?
How to do null check in constructor and properties in C#?
I am getting a warning from the compiler about my code. I don’t know why because I have checked it in the property setter. If I check this in the constructor, the warning disappears, but then I have to write the same code twice, which is not good. Can someone explain to me how I should write this class? I want to know what is the right way to do this?