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
?
This question presupposes a false premise.
Max(int x, int y)
Max(ref int x, ref int y)
Max(out int x, out int y)
are all overloads of a method named Max
. However, note that only one of the last two may be present in any given class definition. From the specification §3.6:
The signature of a method consists of the name of the method, the number of type parameters and the type and kind (value, reference, or output) of each of its formal parameters, considered in the order left to right. For these purposes, any type parameter of the method that occurs in the type of a formal parameter is identified not by its name, but by its ordinal position in the type argument list of the method. The signature of a method specifically does not include the return type, the
params
modifier that may be specified for the right-most parameter, nor the optional type parameter constraints.
[...]
Although
out
andref
parameter modifiers are considered part of a signature, members declared in a single type cannot differ in signature solely byref
andout
. 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 without
modifiers were changed toref
modifiers. For other purposes of signature matching (e.g., hiding or overriding),ref
andout
are considered part of the signature and do not match each other. (This restriction is to allow C# programs to be easily translated to run on the Common Language Infrastructure (CLI), which does not provide a way to define methods that differ solely inref
andout
.)
ref
and out
are the same thing, symantically. The CLR does not differentiate between the two. The C# language is making the distinction. For the CLR, there is only ref
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With