Consider a class with 2 constructors. Let's say that one constructor accepts a dependency, whilst the other accepts a factory for the dependency to be got later:
class ThereCanBeOnlyOneButAtLeastOne
{
readonly Thing? knownThing;
readonly ThingFactory? factory;
public ThereCanBeOnlyOneButAtLeastOne(ThingFactory factory) => this.factory = factory;
public ThereCanBeOnlyOneButAtLeastOne(Thing knownThing) => this.knownThing = knownThing;
public OtherThing CalculateSomething()
{
var theThing = knownThing??factory?.Get();
I would like the static analysis to have worked out that theThing is guaranteed NotNull at this point because at least one of the constructors must have been called. But it doesn't.
Can it?