Difference between ref and out parameters in .NET

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


They're pretty much the same - the only difference is that a variable you pass as an out parameter doesn't need to be initialized but passing it as a ref parameter it has to be set to something.

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

int y;
Foo(ref y); // Error: y should be initialized before calling the method

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.


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 caller. 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).


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 caller 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.

    链接地址: http://www.djcxy.com/p/3630.html

    上一篇: 如何在Java中将数字四舍五入到小数点后n位

    下一篇: .NET中ref和out参数的区别