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

前端 未结 5 1684
臣服心动
臣服心动 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:43

    Instead of going in much detail let me answer you question straight.

    1. Return type of function is specified as Bool hence the function must return True or false
    2. Param has been defined as input parameter hence its value can not be determined at compile time
    3. value of test variable is dependent on param and as per step-2, param value can not be determined at compile time and hence value of test can not be determined at compile time
    4. As value of test is not known as compile time(as per step-3) hence it start looking for a else / default root for if (!test) and hence it throws error.

    thanks

提交回复
热议问题