Pattern matching - variable in scope outside if-block

前端 未结 2 1538
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-15 01:09

I\'m trying to understand why y is in scope in the following example:

static void Main(string[] args)
{
    int x = 1;
    if (x is int y) { }

         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-15 01:53

    That's is something I had found in VS15P4 (or P5) and it was being tracked on GitHub:

    Definite assignment versus pattern-matching when pattern always matches #14651

    Putting aside all the discussions, it is a scoping issue which occurs when the pattern always matches, as the title suggests.

    I thought it was fixed before RC so I haven't actually tested afterwards but it seems the issue still persists.

    Let's see if it will be fixed in the actual release, which is tomorrow :)

提交回复
热议问题