Method overloading with values ​​and reference parameter types

I have the following code:

class Calculator { public int Sum(int x, int y) { return x + y; } public int Sum(out int x, out int y) { x = y = 10; return x + y; } } class Program { static void Main(string[] args) { int x = 10, y = 20; Calculator calculator = new Calculator(); Console.WriteLine ( calculator.Sum ( x , y ) ); Console.WriteLine ( calculator.Sum ( out x , out y ) ); } } 

This code works well even though the method signature differs only in the out keyword.

But the following code did not work:

 class Calculator { public int Sum(ref int x, ref int y) { return x + y; } public int Sum(out int x, out int y) { x = y = 10; return x + y; } } class Program { static void Main(string[] args) { int x = 10, y = 20; Calculator calculator = new Calculator(); Console.WriteLine ( calculator.Sum ( ref x , ref y ) ); Console.WriteLine ( calculator.Sum ( out x , out y ) ); } } 

Why is this code not working? Are keywords such as ref and part of the method signature?

+8
c # method-overloading
source share
4 answers

out parameter modifier (C # link)

Although the ref and out keywords cause different runtime behavior, they are not considered part of the method signature at compile time. Therefore, methods cannot be overloaded if the only difference is that one method takes a ref argument and the other produces an argument.

Also see: ref (link to C # )

Members of the class cannot have signatures that differ only in ref and out. A compiler error if the only difference between the two type members is that one of them has a ref parameter and the other has an out parameter.

+10
source share

To quote a little differently than the other answers, this is from section 3.6 of the C # 5 specification, which I find more clear and precise than the "reference guide":

Although out and ref parameter modifiers are considered part of the signature, members declared in the same type cannot distinguish signatures only by ref and out. A compile-time error occurs if two members are declared in the same type with signatures that would be the same if all parameters in both methods with out modifiers were changed to ref modifiers. For other purposes of the signature (for example, hiding or overriding), ref and out are considered part of the signature and do not coincide with each other. (This limitation is to allow C # programs to be easily translated to work in the Common Language Infrastructure (CLI), which does not provide a way to define methods that differ only in ref and out .)

Note that the type of the dynamic parameter is a bit similar here - as far as the CLR is concerned, it is just a parameter of type object , so this is not valid:

 void InvalidOverload(object x) {} void InvalidOverload(dynamic x) {} 

In this case, however, for a method with a parameter type, dynamic great for overriding one with a parameter type of object or vice versa - the important point is that they are equivalent to the caller, while this does not apply to out / ref .

+6
source share

This is by specification. According to the MSDN page, exit the parameter modifier (C # link)

Although the ref and out keywords cause different runtime behavior, they are not considered part of the method signature at compile time. Therefore, methods cannot be overloaded, unless the difference is that one method takes a ref argument and the other takes an out argument. The following code, for example, will not compile:

+1
source share

this is basically a compilation error, since ref and out are almost the same. both values ​​are almost the same, but the value you pass with the out parameter should not be initialized.

-one
source share

All Articles