Neither can I understand the book says that overloading methods is impossible with the help of ref

Book

Although I did it

 Another an = new Another(); int a = 0; an.SomeMethod(a); an.SomeMethod(ref a); class Another { public void SomeMethod(int a) { a = 1; } public void SomeMethod(ref int a) { a = 1; } } 

Maybe something is not understood?

    1 answer 1

    Perhaps this is not the best translation. Actually this situation is forbidden:

     class Another { public void SomeMethod(out int a) { a = 1; } public void SomeMethod(ref int a) { a = 1; } } 

    This produces a CS0063 error:

    Cannot define overloaded methods.


    The reason is that at the CLR out level does not exist, and is encoded as ref . So if it were allowed, from the CLR’s point of view, it would be two identical signatures. The difference of out from ref (that is, the fact that the out parameter must be initialized inside the method) is an internal C # rule, the CLR does not check this.

    • Thank you so much ! Nice to see the correct and detailed answer. - Eugene
    • @ Eugene: Please! - VladD