简体   繁体   中英

How should I interpret the "null check pattern" in C#

I have some code that effectively does this:

    private void DoStuff(int? a)
    {
        int c = 0;
        if (a is int b)
        {
            c = b;
        }
    }

But a is int b gives me a warning:

Use not null pattern instead of a type check succeeding on any not-null value

Using Resharper's suggestion "Use null check pattern" autocorrects this code as follows, causing the warning message to disappear:

   private void DoStuff(int? a)
    {
        int c = 0;
        if (a is { } b)
        {
            c = b;
        }
    }

That's great and all but now I don't understand the code I'm writing. How should I interpret if(a is {} b) in the english language?

Is it saying "if a is not null set b to a 's non-null value"?

Or is {} a shorthand for "the underlying type of a " (ie int )?

Is there anything I can put inside the braces, or do the braces alone have their own meaning?

Anything to help me understand what this code really means would be appreciated. Thank you.

The a is { } is an example of using is to match an expression against a pattern... reference

The is operator checks if the result of an expression is compatible with a given type. For information about the type-testing is operator, see the is operator section of the Type-testing and cast operators article.

Beginning with C# 7.0, you can also use the is operator to match an expression against a pattern, as the following example shows:

static bool IsFirstFridayOfOctober(DateTime date) =>
    date is { Month: 10, Day: <=7, DayOfWeek: DayOfWeek.Friday }

The a is { } b is an example of using a declaration pattern to declare a new local variable, scoped to the if block. reference

You use declaration and type patterns to check if the run-time type of an expression is compatible with a given type. With a declaration pattern, you can also declare a new local variable. When a declaration pattern matches an expression, that variable is assigned a converted expression result


I highly recommend Sharplab.io to investigate the optimisations that take place during C# compilation. In your provided example:

private void DoStuff(int? a)
{
    int c = 0;
    if (a is { } b)
    {
        c = b;
    }
}

Equates to:

private void DoStuff(Nullable<int> a)
{
    int num = 0;
    int valueOrDefault = default(int);
    int num2;
    if (a.HasValue)
    {
        valueOrDefault = a.GetValueOrDefault();
        num2 = 1;
    }
    else
    {
        num2 = 0;
    }
    if (num2 != 0)
    {
        num = valueOrDefault;
    }
}

So basically a is { } b is saying; does a match any pattern (ie not null) if yes, assign it to a local variable b .


Personally I think that whatever code you write should be as readable as possible for everyone. If you are confused by the code you have written then don't write it, write something that the person taking over from you will understand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM