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?
c # method-overloading
Brahim boulkriat
source share