小编Mic*_*ito的帖子

为什么这段代码不可达?

我发现了一个案例,我有一些我认为无法访问且未被检测到的代码.编译器和Visual Studio都不会发出警告.

考虑以下代码:

enum Foo { A, B, C }
class Bar { public Foo type; }

static class Program
{
    private static void Main()
    {
        var bar = new Bar { type = Foo.A };

        if (bar.type == Foo.B)
        {
            Console.WriteLine("lol");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

显然,程序不会打印出"lol",因为if语句中的条件为false.我不明白为什么没有为无法访问的代码发出警告.我唯一的假设是,如果你在多线程程序中遇到竞争条件,那么这可能是可以达到的.它是否正确?

.net c# unreachable-code

49
推荐指数
3
解决办法
9483
查看次数

标签 统计

.net ×1

c# ×1

unreachable-code ×1