3

Any succinct explanations?

Also Answered in: Difference between ref and out parameters in .NET

Community
  • 1
  • 1
Newbie
  • 7,031
  • 9
  • 60
  • 85
  • Duplicate question: http://stackoverflow.com/questions/135234/difference-between-ref-and-out-parameters-in-net – Pat Feb 05 '09 at 17:17

5 Answers5

19

For the caller:

  • For a ref parameter, the variable has to be definitely assigned already
  • For an out parameter, the variable doesn't have to be definitely assigned, but will be after the method returns

For the method:

  • A ref parameter starts off definitely assigned, and you don't have to assign any value to it
  • An out parameter doesn't start off definitely assigned, and you have to make sure that any time you return (without an exception) it will be definitely assigned

So:

int x;
Foo(ref x); // Invalid: x isn't definitely assigned
Bar(out x); // Valid even though x isn't definitely assigned
Console.WriteLine(x); // Valid - x is now definitely assigned

...

public void Foo(ref int y)
{
    Console.WriteLine(y); // Valid
    // No need to assign value to y
}

public void Bar(out int y)
{
    Console.WriteLine(y); // Invalid: y isn't definitely assigned
    if (someCondition)
    {
        // Invalid - must assign value to y before returning
        return;
    }
    else if (someOtherCondition)
    {
        // Valid - don't need to assign value to y if we're throwing
        throw new Exception();
    }
    else
    {
        y = 10;
        // Valid - we can return once we've definitely assigned to y
        return;
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    What's really interesting though is the CLR has no concept of ref. It's a c# semantic. – JaredPar Feb 05 '09 at 17:23
  • Don't you mean that the CLR understands ref but not out? – Jon Skeet Feb 05 '09 at 17:35
  • 1
    (I thought that was the case, anyway - that the C# compiler just adds the [Out] attribute for out methods, and makes appropriate checks, but that it was otherwise just a ref parameter.) – Jon Skeet Feb 05 '09 at 17:37
  • @Jon, doh! Yes your are correct. I have it backwards – JaredPar Feb 05 '09 at 18:21
  • Thank goodness for that. The whole of my knowledge, identity, soul and being is derived from that fact. I was trembling on the brink... (Seriously though, I would have been surprised and slightly disturbed.) – Jon Skeet Feb 05 '09 at 18:23
3

Most succinct way of viewing it:

ref = inout

out = out

Andrew Coleson
  • 10,100
  • 8
  • 32
  • 30
2

See this article on MSDN. They both accomplish subtly different things, really.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Alex Fort
  • 18,459
  • 5
  • 42
  • 51
1

Ref and out parameter passing modes are used to allow a method to alter variables passed in by the caller. The difference between ref and out is subtle but important. Each parameter passing mode is designed to apply to a slightly different programming scenario. The important difference between out and ref parameters is the definite assignment rules used by each.

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.

source: MSDN

David P
  • 3,604
  • 3
  • 37
  • 54
1

From the MSDN article that Alex mentions,

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.

So to sum up, inside the method you can consider ref parameters to be set, but not out parameters - you must set these. Outside the method they should act the same.

Community
  • 1
  • 1
tooleb
  • 612
  • 3
  • 6
  • 14