Difference between ref and out?

  • Thread starter Thread starter Jon Davis
  • Start date Start date
J

Jon Davis

I've been using the ref keyword as a way of passing strings to methods as
references so that in the method I don't have to manually return the
modified "version" of what was passed into the method, I just have to assign
a new value to the ref'd argument. However, I've learned that the 'out'
keyword seems to do something just like this.

I'm kind of confused; what's the practical difference between 'ref' and
'out' for my intents?

Thanks,
Jon
 
Jon said:
I'm kind of confused; what's the practical difference between 'ref'
and 'out' for my intents?

A "ref" parameters need initialization BEFORE you call the function; "out"
does not.


The following will not compile:

string a;
f(ref a);


The following will compile:


string a;
g(out a);
 
A variable to be sent as ref parameter must be initialized.
It is intended to be changed in the method to which it is passed.

A variable to be sent as out parameter dont need to be initialized
before being passed to a method, because it must be assigned in that method.
It is not intended to be changed, but intended to be assigned(or reassigned)
in the method to which it is passed.
 
Hi,
ref parameters should be initialized by the caller before getting passed.
Whereas, out parameters need not be initialized by the caller and the called
method should set values for such out parameters.

I've been using the ref keyword as a way of passing strings to methods as
references so that in the method I don't have to manually return the
modified "version" of what was passed into the method, I just have to assign
a new value to the ref'd argument. However, I've learned that the 'out'
keyword seems to do something just like this.

I'm kind of confused; what's the practical difference between 'ref' and
'out' for my intents?

Thanks,
Jon
 
Thanks for the responses, guys. So essentially, ref and out can be used
interchangeably, depending on whether the object was previously initialized
as well as whether or not the method is required to assign/reassign a value
(whereas a method using ref can ignore it if it so chooses), correct?

Jon
 
Hello Shiva,
Hi,
ref parameters should be initialized by the caller before getting
passed. Whereas, out parameters need not be initialized by the caller
and the called method should set values for such out parameters.

Replace "should" with "must" and it is correct!
 
That is correct, Jon.

However, i think it is important to use ref and out as intended.
Typical ref usage:
void Increase ( ref int value )
{
value ++;
}


Typical out usage:
void GetCount ( out int value )
{
value = 123;
}
 
Jon,

JD> I'm kind of confused; what's the practical difference between
JD> 'ref' and 'out' for my intents?

The ref modifier has [in, out] semantics, while the out modifier has
[out] semantics.

This means that, from inside the method body, you cannot read an out
argument before you assign to it. Furthermore, all code paths should
assign to an out argument.

void Foo(bool f, out int i) {
int j = i; // ERROR: cannot read from i

if (f)
{
i = 5;
}
// ERROR: not all code paths assign to i
}

A ref argument poses restrictions on the caller's site, since ref
arguments should be initialized before being sent to a method.

void Bar(ref i) {
i++;
}

void CallBar() {
int i;

Bar(ref i); // ERROR: i is uninitialized
}

HTH,

Stefan
 
Thanks! :-)

Hello Shiva,
Hi,
ref parameters should be initialized by the caller before getting
passed. Whereas, out parameters need not be initialized by the caller
and the called method should set values for such out parameters.

Replace "should" with "must" and it is correct!
 
This means that, from inside the method body, you cannot read an out
argument before you assign to it.

Ooooohhhh!! That IS different than I was thinking. I think then that where
I was using ref I should still be using ref. :)

Thanks!!

Jon


Stefan Holdermans said:
Jon,

JD> I'm kind of confused; what's the practical difference between
JD> 'ref' and 'out' for my intents?

The ref modifier has [in, out] semantics, while the out modifier has
[out] semantics.

This means that, from inside the method body, you cannot read an out
argument before you assign to it. Furthermore, all code paths should
assign to an out argument.

void Foo(bool f, out int i) {
int j = i; // ERROR: cannot read from i

if (f)
{
i = 5;
}
// ERROR: not all code paths assign to i
}

A ref argument poses restrictions on the caller's site, since ref
arguments should be initialized before being sent to a method.

void Bar(ref i) {
i++;
}

void CallBar() {
int i;

Bar(ref i); // ERROR: i is uninitialized
}

HTH,

Stefan
 
Thanks for the responses, guys. So essentially, ref and out can be used
interchangeably, depending on whether the object was previously initialized
as well as whether or not the method is required to assign/reassign a value
(whereas a method using ref can ignore it if it so chooses), correct?

Interchangeably only if you're only interested in the OUT portion - an
"OUT" parameter CANNOT contain any value coming into the procedure, so
you CANNOT pass any information INTO the procedure through an OUT
parameter.

From the point of view of what's happening once the procedure call is
completed, then yes - both are pretty much the same. You can (and in
the case of the "out" parameter MUST) assign a value to the parameter
inside the procedure called, and the callee can then use / check /
inspect / use that new value.

Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Back
Top