GrandMasterFlush
GrandMasterFlush

Reputation: 6409

Why do I need to prefix params with out or ref?

If I'm calling a method that uses a out or ref parameter why do you need to mark the parameters with out or ref when calling the method? i.e.

DateTime.TryParse(myDate, out dateLogged)

I had thought this was something to reduce ambiguity when operator overloading but if I create two methods like so:

void DoStuff(int x, out int y)
{
    y = x + 1;
}

void DoStuff(int x, ref int y)
{
    y = x + 1;
}

Then Visual Studio reports "Cannot define overloaded method 'DoStuff' because it differs from another method only on ref and out", so I couldn't do that anyway.

EDIT:

After a bit more research it seems that I would need to specify out or ref when calling a method if it there were two overloaded methods declared like this:

    void DoStuff(int x, out int y)

    void DoStuff(int x, int y)

Upvotes: 3

Views: 558

Answers (5)

Jodrell
Jodrell

Reputation: 35726

So the people reading your code know what you are doing, it makes it clear that the passed variable could change.

It has additional benefit of stopping you from doing it by accident.

Having maintained code on platforms that pass by ref implicitly, or even by default, I would say that typing 3 charachters is a small price to pay for the clarity (and possibly sanity) it provides.

Upvotes: 1

Giovanni Galbo
Giovanni Galbo

Reputation: 13081

Ref and Out are really the same thing. The difference is that the out keyword makes the compiler check that the variable is assigned before returning from the method.

EDIT

Sorry, misunderstood the question, I thought you were asking why c# wouldn't allow you to have 2 methods with the same signature, with only a difference in parameter modifiers. See @Adam Robinson's answer; it's to ensure that the caller knows what may happen (or will happen in the case of out) to their variable after returning from the method.

Upvotes: 2

Justin
Justin

Reputation: 86789

C# makes you mark a parameter as ref / out as a warning / pointer to the programmer that something different is happening with these parameters and that the value passed may change. For example in this case:

int myInt = 12;
string myString = Fizz(myInt);
// What is the value of myInt at this point?

I know looking at the above example that the value of myInt is 12, however this wouldn't be the case if out and ref parameters were implicit.

Upvotes: 1

Adam Robinson
Adam Robinson

Reputation: 185693

It's there so that you're explicitly consenting to the possibility that the method you're calling could modify the value of the variable itself. This is a C# language requirement; other languages like VB.NET do not require you to decorate the actual method call, just the method signature.

Upvotes: 10

TJHeuvel
TJHeuvel

Reputation: 12618

Your case could be considered a hack. It's purpose is to allow a method to change the value of an argument.

For example:

void someMethod(ref i)
{
 i = 1;
}


int i = 0;
someMethod(ref i); //i now becomes 1, without the ref it would stay 0.

Upvotes: 0

Related Questions