Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReSharper suggests changing pattern matching code to object pattern

Tags:

c#

resharper

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?

like image 707
Jogge Avatar asked Oct 17 '19 07:10

Jogge


1 Answers

{} 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.

like image 131
p__d Avatar answered Oct 22 '22 02:10

p__d