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" (i.e. 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.