问题
ReSharper suggests changing the following code:
if (MyString is string myString)
{
//...
}
to object pattern:
if (MyString is { } myString)
{
//...
}
It says:
The source expression is always of pattern's type, matches on all non-null values.
I have never seen this syntax before, and I cannot find any documentation of it. What is it and what does it do?
回答1:
{}
stand for not null
, but not invokes !=
operator, so it's safe like is null
operator which not invoke ==
operator.
There is lack of information about it. I found thread on github about it. This is a preview feature for now i think.
来源:https://stackoverflow.com/questions/58427535/resharper-suggests-changing-pattern-matching-code-to-object-pattern