.NET compiler and “Not all code paths return a value”

前端 未结 5 1668
臣服心动
臣服心动 2020-12-11 11:24

Why in code like below is the .NET compiler not able to establish that all code paths do return a value?

bool Test(bool param) {
    bool test = true;
    if         


        
5条回答
  •  抹茶落季
    2020-12-11 11:51

    From Eric Lippert's blog:

    The reachability analyzer is not very smart. It does not realize that there are only two possible control flows and that we've covered all of them with returns.

    (The blog post is on switch statements, but I guess the reachability analyzer isn't much smarter for if statements.)

提交回复
热议问题