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 (param)
test = false;
else
test = false;
if (!test)
return false;
}
error CS0161: Not all code paths return a value!
The code can be refactored - but the compiler is not suggesting that. Yet all return paths are covered - so why does the compiler complain that they are not?
Edit: I guess the conclusion here is that:
(error CS0161) + (all code paths obviously return a value) => refactor code.
Once you get the habit of that translation I guess everything is ok.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…