When to use ref vs out
Someone asked me the other day when they should use the parameter keyword out
instead of ref
. While I (I think) understand the difference between the ref
and out
keywords (that has been asked before) and the best explanation seems to be that ref
== in
and out
, what are some (hypothetical or code) examples where I should always use out
and not ref
.
Since ref
is more general, why do you ever want to use out
? Is it just syntactic sugar?
You should use out
unless you need ref
.
It makes a big difference when the data needs to be marshalled eg to another process, which can be costly. So you want to avoid marshalling the initial value when the method doesn't make use of it.
Beyond that, it also shows the reader of the declaration or the call whether the initial value is relevant (and potentially preserved), or thrown away.
As a minor difference, an out parameter needs not be initialized.
Example for out
:
string a, b;
person.GetBothNames(out a, out b);
where GetBothNames is a method to retrieve two values atomically, the method won't change behavior whatever a and b are. If the call goes to a server in Hawaii, copying the initial values from here to Hawaii is a waste of bandwidth. A similar snippet using ref:
string a = String.Empty, b = String.Empty;
person.GetBothNames(ref a, ref b);
could confuse readers, because it looks like the initial values of a and b are relevant (though the method name would indicate they are not).
Example for ref
:
string name = textbox.Text;
bool didModify = validator.SuggestValidName(ref name);
Here the initial value is relevant to the method.
Use out to denote that the parameter is not being used, only set. This helps the caller understand that you're always initializing the parameter.
Also, ref and out are not just for value types. They also let you reset the object that a reference type is referencing from within a method.
You're correct in that, semantically, ref
provides both "in" and "out" functionality, whereas out
only provides "out" functionality. There are some things to consider:
out
requires that the method accepting the parameter MUST, at some point before returning, assign a value to the variable. You find this pattern in some of the key/value data storage classes like Dictionary<K,V>
, where you have functions like TryGetValue
. This function takes an out
parameter that holds what the value will be if retrieved. It wouldn't make sense for the caller to pass a value into this function, so out
is used to guarantee that some value will be in the variable after the call, even if it isn't "real" data (in the case of TryGetValue
where the key isn't present). out
and ref
parameters are marshaled differently when dealing with interop code Also, as an aside, it's important to note that while reference types and value types differ in the nature of their value, every variable in your application points to a location of memory that holds a value , even for reference types. It just happens that, with reference types, the value contained in that location of memory is another memory location. When you pass values to a function (or do any other variable assignment), the value of that variable is copied into the other variable. For value types, that means that the entire content of the type is copied. For reference types, that means that the memory location is copied. Either way, it does create a copy of the data contained in the variable. The only real relevance that this holds deals with assignment semantics; when assigning a variable or passing by value (the default), when a new assignment is made to the original (or new) variable, it does not affect the other variable. In the case of reference types, yes, changes made to the instance are available on both sides, but that's because the actual variable is just a pointer to another memory location; the content of the variable--the memory location--didn't actually change.
Passing with the ref
keyword says that both the original variable and the function parameter will actually point to the same memory location. This, again, affects only assignment semantics. If a new value is assigned to one of the variables, then because the other points to the same memory location the new value will be reflected on the other side.
上一篇: 使用C#将参数传递为参数
下一篇: 何时使用ref vs out