Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a technical reason for requiring the "out" and "ref" keywords at the caller?

Tags:

c#

When calling a method with a ref or out parameter, you have to specify the appropriate keyword when you call the method. I understand that from a style and code quality standpoint (as explained here for example), but I'm curious whether there is also a technical need for the keywords to be specified in the caller.

For example:

static void Main()
{
    int y = 0;
    Increment(ref y); // Is there any technical reason to include ref here?
}

static void Increment(ref int x)
{
    x++;
}
like image 821
Jason Watkins Avatar asked Apr 19 '13 02:04

Jason Watkins


1 Answers

The only technical reason I could think of is overload resolution: you could have

static void Increment(ref int x)

and also

static void Increment(int x)

This is allowed; without ref in the call, the compiler wouldn't be able to tell them apart.

like image 152
Sergey Kalinichenko Avatar answered Oct 01 '22 22:10

Sergey Kalinichenko