Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementing a nullable guard method for a specific member #3447

Closed
bitbonk opened this issue May 11, 2020 · 2 comments
Closed

implementing a nullable guard method for a specific member #3447

bitbonk opened this issue May 11, 2020 · 2 comments

Comments

@bitbonk
Copy link
Contributor

bitbonk commented May 11, 2020

Consider the following code:

private Foo? fooMember;

public void DoSomethingWithFoo()
{
    this.VerifyFooNotNull();
    this.fooMember.DoSomething(); // Warning CS8602: Dereference of a possibly null reference.
}

private void VerifyFooNotNull()
{
     if (this.fooMember == null)
    {
        throw new InvalidOperationException("Foo must be initialized");
    }
}

Is there an attribute or similar I could apply to VerifyFooNotNull so that the CS8602 warning goes away?

@KalleOlaviNiemitalo
Copy link

In C# 9, MemberNotNullAttribute from dotnet/roslyn#41438 seems to cover this.

@333fred
Copy link
Member

333fred commented May 11, 2020

Yep, that should be covered by MemberNotNull. I'm going to close this as a duplicate, let me know if that's not quite the same as what you're looking for and I can reopen. #3297

@333fred 333fred closed this as completed May 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants