Can a method assert that a field is not null afterwards?

I'm going to try MemberNotNullWhen as suggested by DerMave again, but I've had bad luck with that. I don't want to go into it because it was months ago and I'm sure if I head to that part of the code I'll realize my context was different.

This is a little clumsy but you may consider:

private YourDataType ThrowIfUnconfigured()
{
    return MyThingy switch
    {
        null => throw new InvalidOperationException(...);
        _ => MyThingy;
    }
}

The somewhat awkward thing is your call site becomes:

public bool IsAvailable
{
    get
    {
        var thingy = ThrowIfUnconfigured();

        // ... use the local from now own
    }
}

I'm not sure if the attributes work for this but I'm sure that little extra step will.

/r/csharp Thread