Really impossible to use return type overloading?

前端 未结 7 1340
梦如初夏
梦如初夏 2020-11-30 11:20

I made a small DLL in MSIL with two methods:

float AddNumbers(int, int)
int AddNumbers(int, int)

As some of you might know, MSIL allows you

7条回答
  •  一向
    一向 (楼主)
    2020-11-30 11:57

    Yes it really is not possible in C#, I know C++ does not allow this either, it has to do with the way a statement is interpreted:

    double x = AddNumbers(1, 2);
    

    The rule here is that the assignment is right-associative, meaning the expression on the right is completely evaluated first and only then is the assignment considered, applying implicit conversions where necessary.

    There is no way for the compiler to determine which version is most appropriate. Using some arbitrary rule would just invite hard to find errors.

    It is related to this simple statement:

    double y = 5 / 2;  // y = 2.0
    

提交回复
热议问题