How to deal with the "Non-nullable property 'S2' must contain a non-null value"-warning for the private constructor in the following example:
#nullable enable
class X {...}
class Y {...}
class Z {...}
class C {
public string S1 { get; }
public string S2 { get; }
private C(Z z) => S1 = GenerateS1FromZ(z);
public C(Z z, X x) : this(z) => S2 = GenerateS2FromX(x);
public C(Z z, Y y) : this(z) => S2 = GenerateS2FromY(y);
private static string GenerateS1FromZ(Z z) { ... }
private static string GenerateS2FromX(X x) { ... }
private static string GenerateS2FromY(Y y) { ... }
}
S1 must be initialized in a constructor, since it has no setter. S2 can be initialized in two ways, so there are two public constructors.
Assume that S1 and S2 are just placeholders for a few more properties. Therefore, it is not practical to pass the value for S2 to the private constructor as parameter or to initialize S1 in both public constructors.
Under this assumption the code above is ok I think, but the non-nullable-warning does not work as I wish. One could add S2=null!; in the private constructor to avoid the warning, but that seems to be a hack.