Why can't we define a variable inside an if statement?

前端 未结 5 1075
余生分开走
余生分开走 2020-12-01 04:13

Maybe this question has been answered before, but the word if occurs so often it\'s hard to find it.

The example doesn\'t make sense (the expression is

5条回答
  •  我在风中等你
    2020-12-01 04:46

    Try C#7's Pattern Matching.

    Using your example:

    if (new StringBuilder("test") is var sb && sb != null) {
        Console.WriteLine(sb);
    }
    

提交回复
热议问题