C# 7 pattern matching semantics

安稳与你 提交于 2019-12-02 01:39:29

问题


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

First the if based block

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

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?


回答1:


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



来源:https://stackoverflow.com/questions/41504414/c-sharp-7-pattern-matching-semantics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!