3

Any succinct explanations?

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


6 답변


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;
    }
}


  • What's really interesting though is the CLR has no concept of ref. It's a c# semantic. - JaredPar
  • Don't you mean that the CLR understands ref but not out? - Jon Skeet
  • (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
  • @Jon, doh! Yes your are correct. I have it backwards - JaredPar
  • 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

3

Most succinct way of viewing it:

ref = inout

out = out



2

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


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


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.


0

Check out this Jon Skeet article about params in c#:

http://www.yoda.arachsys.com/csharp/parameters.html

Linked


Related

Latest