tags:

views:

10755

answers:

14

What is the difference between ref and out parameters in .NET? What are the situations where one can be more useful than the other? Can anybody illustrate with a code snippet where one can be used and another can't?

A: 

ref will probably choke on null since it presumably expects to be modifying an existing object. out expects null, since it's returning a new object.

Joel Coehoorn
Foo(ref null) won't compile, butstring bar = null; Foo(ref bar); is fine.
James Curran
Choke on null constant? As James Curran you can't pass a constant in a ref argument.
Andrei Rinea
I didn't mean a null constant: I meant a variable with a null value.
Joel Coehoorn
+3  A: 

Ref parameters aren't required to be set in the function, whereas out parameters must be bound to a value before exiting the function. Variables passed as out may also be passed to a function without being initialized.

Patrick
A: 

The out keyword causes arguments to be passed by reference. This is like the ref keyword, except that ref requires that the variable be initialized before it is passed.

Source

Adam Hughes
+1  A: 

out parameters are initialized by the method called, ref parameters are initialized before calling the method. Therefore, out parameters are used when you just need to get a secondary return value, ref parameters are used to get a value and potentially return a change to that value (secondarily to the main return value).

bdukes
+44  A: 

They're pretty much the same - the only difference is that a variable you pass as an out parameter doesn't need to be initialised, and the method using the ref parameter has to set it to something.

int x;
Foo(out x); // OK

int y;
Foo(ref y); // Error

Ref parameters are for data that might be modified, out parameters are for data that's an additional output for the function (eg int.TryParse) that are already using the return value for something.

Khoth
just wat i was looking for, a consise descripton, with not too muxh of thory, and a small code snippet to illustrate it. Thanks!
ashwnacharya
Also, out parameters must be set during the execution of the callee before returning to the caller, which is in opposition to ref, which must be set by the caller before calling the callee.
Jesse C. Slicer
This is really helpful..Thanks
Vijay Balkawade
+24  A: 

Why does C# have both 'ref' and 'out'?

The caller of a method which takes an out parameter is not required to assign to the variable passed as the out parameter prior to the call; however, the callee is required to assign to the out parameter before returning.

...

In contrast ref parameters are considered initially assigned by the callee. As such, the callee is not required to assign to the ref parameter before use. Ref parameters are passed both into and out of a method.

So, out means out, while ref is for in and out.

These correspond closely to the [out] and [in,out] parameters of COM interfaces, the advantages of out parameters being that callers need not pass a pre-allocated object in cases where it is not needed by the method being called - this avoids both the cost of allocation, and any cost that might be associated with marshaling (more likely with COM, but not uncommon in .NET).

Derek Park
Great explanation and tie in with COM. Oh the days of COM... joy.
Scott Saad
+1  A: 

The ref keyword is used to pass values by reference. (This does not preclude the passed values being value-types or reference types). Output parameters specified with the out keyword are for returning values from a method.

One key difference in the code is that you must set the value of an output parameter within the method. This is not the case for ref parameters.

For more details look at http://www.blackwasp.co.uk/CSharpMethodParameters.aspx

BlackWasp
A: 

This article has some good examples.

The basic difference outlined is that out parameters don't need to be initialized when passed in, while ref parameters do.

casademora
A: 

out and ref are exactly the same with the exception that out variables don't have to be initialized before sending it into the abyss. I'm not that smart, I cribbed that from the MSDN library :).

To be more explicit about their use, however, the meaning of the modifier is that if you change the reference of that variable in your code, out and ref will cause your calling variable to change reference as well. In the code below, the ceo variable will be a reference to the newGuy once it returns from the call to doStuff. If it weren't for ref (or out) the reference wouldn't be changed.

private void newEmployee()
{
    Person ceo = Person.FindCEO();
    doStuff(ref ceo);
}

private void doStuff(ref Person employee)
{
    Person newGuy = new Person();
    employee = newGuy;
}
Jacob Proffitt
+3  A: 

[ref] and [out] both allow the called method to modify a parameter. The difference between them is what happens before you make the call.

  • [ref] means that the parameter has a value on it before going into the function. The called function can read and or change the value any time. The parameter goes in, then comes out

  • [out] means that the parameter has no official value before going into the function. The called function must initialize it. The parameter only goes out

Here's my favorite way to look at it: [ref] is to pass variables by reference. [out] is to declare a secondary return value for the function. It's like if you could write this:

// This is not C#
public (bool, string) GetWebThing(string name, [ref] Buffer paramBuffer);

// This is C#
public bool GetWebThing(string name, [ref] Buffer paramBuffer, [out] string actualUrl);

Here's a more detailed list of the effects of each alternative:

Before calling the method:

[ref]: The called must set the value of the parameter before passing it to the called method.

[out]: The caller method is not required to set the value of the argument before calling the method. Most likely, you shouldn't. In fact, any current value is discarded.

During the call:

[ref]: The called method can read the argument at any time.

[out]: The called method must initialize the parameter before reading it.

Remoted calls:

[ref]: The current value is marshalled to the remote call. Extra performance cost.

[out]: Nothing is passed to the remote call. Faster.

Technically speaking, you could use always [ref] in place of [out], but [out] allows you to be more precise about the meaning of the argument, and sometimes it can be a lot more efficient.

Euro Micelli
+1  A: 

Example for OUT : Variable gets value initialized after going into the method. Later the same value is returned to the main method.

namespace outreftry { class outref { static void Main(string[] args) { yyy a = new yyy(); ;

        int i; // u can try giving int i=100 but is useless as that value is not passed into the method. Only variable goes into the method and gets changed its value and comes out. 

        a.abc(out i);

        System.Console.WriteLine(i);
    }
}
class yyy
{

    public void abc(out int i)
    {

        i = 10;

    }

}

} Output:

10

===============================================

Example for Ref : Variable should be initialized before going into the method. Later same value or modified value will be returned to the main method.

namespace outreftry { class outref { static void Main(string[] args) { yyy a = new yyy(); ;

        int i = 0;

        a.abc(ref i);

        System.Console.WriteLine(i);
    }
}
class yyy
{

    public void abc(ref int i)
    {
        System.Console.WriteLine(i);
        i = 10;

    }

}

} Output: 0

    10

=================================

Hope its clear now.

+2  A: 

The obvious answer: ref means in and out, out meant out only. This has consequences for C#'s definitely assigned tracking:

void foo(ref int x, out int y)
{
    x = y; // Error: y is not definitely assigned
    y = x; // OK        
}

...
   int a, b;
   foo(
      ref a,  // Error: a is not definitely assigned
      out b); // OK
Henk Holterman
+1  A: 
  • A ref variable needs to be initialized before passing it in.
  • An out variable needs to be set in your function implementation
  • out parameters can be thought of as additional return variables (not input)
  • ref parameters can be thought of as both input and output variables.
Brian R. Bondy
+1  A: 

out specifies that the parameter is an output parameters, i.e. it has no value until it is explicitly set by the method.

ref specifies that the value is a reference that has a value, and whose value you can change inside the method.

Alan