简体   繁体   中英

C# 7 pattern matching semantics

I have the two blocks of code that I would like to think are equal:

First the if based block 基于IF的代码

Then the exact same but converted to switch case over types. (sorry for bad Resharper red squiggly markers, Resharper doesnt understand this yet)

基于SWITCH的代码

The switch based code will throw a Null reference exception on the first return Actor.Done which is not null.

Is this a pattern match bug or is there some semantics that I am missing here?

[edit] I've found a fix for it..

Changing this:

case MessageEnvelope env:

to

case MessageEnvelope _:
    var env = m as MessageEnvelope;

Makes it all work. So that leaves me with the question, arn't those lines supposed to be exctly the same?

This is a known bug when you capture (in a lambda) a pattern variable that was declared in a switch statement. See https://github.com/dotnet/roslyn/issues/16066

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