Using ref if already reference variable

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

Is there any harm in passing an object into a method with the 'ref'
keyword if the object is already a reference variable? If not, is
there any benefit?
 
Passing a reference to an object by "ref" indicates to the caller that
you might change the reference itself. That is, the caller may call you
with the variable pointing to one object instance, but you might return
with the variable pointing to a different object instance (or null).
Therefore, your calling code cannot trust that the variable used as the
"ref" argument contains a reference to the same object as before, or
even still contains a reference to any object, after the call.

Passing the reference by value (the usual way) indicates that you may
manipulate the object instance itself, but that you can't change to
which object the caller's variable refers.

Passing by ref, your callers cannot use properties or method results
directly as arguments. They must pass variables or fields.
 
"Passing by ref, your callers cannot use properties or method results
directly as arguments. They must pass variables or fields."

I understood your first two paragraphs but not this one. Can you show
me an example?

Also, if you are unconcerned with whether they can set the object to
null or not, is there any other issues to be concerned with (such as
memory issues?)?
 
If you declare a "ref" or "out" parameter to your method, whatever your
callers pass as an argument to that parameter must be able to receive a
return value. Let's say that you declare:

public class MyBusinessObject
{
public string Code;
private string _description;
...
public string Description { get { return this._description; }}

public string SomethingOrOther(int x, int y) { ... }
}

public void MessWithString(ref string stringToChange) { ... }

You _can_ pass the following things to MessWithString:

MyBusinessObject mbo = new MyBusinessObject();
MessWithString(ref mbo.Code);
string anyString = "Hello world.";
MessWithString(ref anyString);

You _cannot_ pass the following things to MessWithString, even though
they return strings. Because they are not variables or fields, there is
no where to put the changed reference:

MessWithString(ref mbo.SomethingOrOther(6, 5));
MessWIthString(ref mbo.Description);
MessWithString(ref "The quick brown fox.");

In all of these cases the compiler will give you an error message.

(By the way, MessWithString is a nasty method with very bad form: you
should always prefer to write a method that returns a value rather than
using ref or out. I wrote it that way only for purposes of
illustration.)

There are no memory advantages to using "ref". You should use it with
reference types only when, for some reason, your method needs to change
the object to which the caller's reference points.
 
What about in a situation like this?

private bool TestMethod(myFirstClass oFirstClass, mySecondClass
oSecondClass, myThirdClass oThirdClass)
{
...code here
}


In this case, I'm not concerned with whether or not they set the value
of those objects to null inside this method. I do want them to have
the ability to modify properties of the three classes within them
(which they can regardless if they pass the parameters with the 'ref'
keyword or not).

What I am concerned with is that I may call this method many, many
times under different threads. From what I understand, that would mean
that each time this method is called a new instance of each class is
created of the variables since they are not passed with the 'ref'
keyword. From a memory point of view, how bad is that?
 
Doug said:
What about in a situation like this?

private bool TestMethod(myFirstClass oFirstClass, mySecondClass
oSecondClass, myThirdClass oThirdClass)
{
...code here
}


In this case, I'm not concerned with whether or not they set the value
of those objects to null inside this method. I do want them to have
the ability to modify properties of the three classes within them
(which they can regardless if they pass the parameters with the 'ref'
keyword or not).

What I am concerned with is that I may call this method many, many
times under different threads. From what I understand, that would mean
that each time this method is called a new instance of each class is
created of the variables since they are not passed with the 'ref'
keyword. From a memory point of view, how bad is that?

NO, a new object is not created(ie copied). A reference to the existing objects are copied.
Your method passed 3 references to 3 objects BY VALUE.
this is very much like passing a pointer as a param in C.

If you use the ref keyword you are now passing the reference to the object BY REFERENCE.
In C this would be like passing a pointer to a pointer.

Bill
 
Back
Top