I have the following code :
class Calculator
{
public int Sum(int x, int y)
{
return x + y;
}
public int Sum(o
To quote slightly differently to the other answers, this is from section 3.6 of the C# 5 specification, which I find rather clearer and more precise than the "reference guide":
Although
out
andref
parameter modifiers are considered part of a signature, members declared in a single type cannot differ in signature solely 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 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
.)
Note that a parameter type of dynamic
is slightly similar here - as far as the CLR is concerned, that's just a parameter of type object
, so this is invalid:
void InvalidOverload(object x) {}
void InvalidOverload(dynamic x) {}
In that case, however, it's fine for a method with a parameter type of dynamic
to override one with a parameter type of object
or vice versa - the important point there is that they're equivalent to callers, whereas that isn't true of out
/ref
.