简体   繁体   中英

Difference between is X and is X _

Before C# 7.0, the following code was valid:

if(x is X)
{
    //...
}

Now, the following is also valid:

if(x is X _)
{
    //...
}

Accoding to TryRoslyn , those code sections generates the same IL. My question is: what's the purpose of the discard operator in pattern matching, if it is not necessary neither useful? Am I missing something here?

This question is not the same as that one, since I'm asking the purpose of the discard operator (wildcard) specifically for the pattern matching ( is operator).

根据Neal Gafter的说法在这种情况下 ,抛弃运算符几乎没有用处,至少到目前为止是没有用的。

This is not useful yet. But when recursive pattern matching is introduced (hopefully in C# 8.0), it will be useful to have discards in part of the pattern.

The details are not finalized, but imagine something like is (1, (_, 3)) or possibly is X (1, Y (_, 3)) .

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