Why ref and out are not sufficient to disambiguate overloaded in C#?

前端 未结 5 1694
终归单人心
终归单人心 2021-01-18 21:41

For example, why this method Max(ref int x, ref int y) is not considered overload of Max(int x, int y)? Why is the same with out?

5条回答
  •  灰色年华
    2021-01-18 21:59

    public void Max(int x, int y)          
    //No Error
    public void Max(ref int x, ref int y)  
    //No Error
    public void Max(out int x, out int y)  
    //cannot define an overloaded method that differs only on parameter modifiers 'out' and 'ref'
    

    First and second method can be overloaded. But the second and third method cannot be overloaded because both ref and out are treated differently at run time but they are treated the same at compile time.

提交回复
热议问题