简体   繁体   中英

Methods overloading with value 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 work well despite that methods signature are differenciated only be the out keyword.

But the following code didn't 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 this code didn't work ? Are keywords like ref and out part of methods signatures?

out parameter modifier (C# Reference)

Although the ref and out keywords cause different run-time 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 takes an out argument.

Also see: ref (C# Reference )

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

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 and ref 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 with out modifiers were changed to ref modifiers. For other purposes of signature matching (eg, hiding or overriding), ref and out 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 in ref and out .)

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 .

This is by specification. According to MSDN page out parameter modifier (C# Reference)

Although the ref and out keywords cause different run-time 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 takes an out argument. The following code, for example, will not compile:

it's basically a compilation error as both ref and out are almost same. both are almost same but a value you pass a out parameter need not be initialised.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM