views:

463

answers:

4

using c#, vs2008, winforms

If i am passing a parameter via a property to a child form from a parent form, or infact via a property to any class, and the the paramater im passing in C# is a reference type created in the parent form, does it get passed as a ref or value by default ?

Eg if i pass a dataset via a property. And if it does get passed by value, can you make it passed by ref via a property ? Or should i just pass it via a method paramater, is that better practice ?

Basicaly i want to retrieve a populated object back to the parent form, and feel passing by ref an object that is created in the parent form is better.

+4  A: 

For reference types, a reference to the variable is passed by value. Unless, of course, you use the ref or out keywords in C# to alter that behaviour.

That means that a DataSet-valued property passes, in actual fact, a reference to a DataSet instance, and passes it by value.

Jon Skeet's "C# in Depth" is a great reference (no pun intended) on these matters.

CesarGon
So if i pass a dataset to a child form as a property paramter, change the content of the dataset, then close the child form, without retrieving the property return value then i should find the changes in the dataset object i created in the parent form. Right ?
Spooky2010
Absolutely right. :-)
CesarGon
@Jon: oops! Thanks for the fix!
CesarGon
Note though, if you change the 'value' of dataset in the called function [datasetCopy = new DataSet();], then the datasetCopy object now points to a different object than the calling function object; thus the original object from the calling function is not affected by subsequent changes to the datasetCopy reference in the called function.
Jess
A: 

C# does not pass by reference in the same way that C++ does. It is more accurate to say that it passes by reference value (for reference types anyways).

Read this by Jon Skeet

Brett Widmeier
+1  A: 

It is important to note that pass by reference in C# has a specific meaning. In the case of a property, the property ends up pointing to the same address as that of the object it was set to. In the case of passing objects to a function, C# uses pass reference by value semantics. That means that the reference itself is copied, so a new pointer points to the same address as the object that was passed. This prevents a function from nullifying any original pointer by setting its parameters to null. To actually pass an original reference, the 'ref' keyword must be used:

class SomeClass
{
  public object MyObjectProperty { get; set; }
}

var someClass = new SomeClass();
object someObject = new object();

someClass.MyObjectProperty = someObject; // Makes MyObjectProperty point to the same location as someObject

In the following case, reference by value semantics are used:

void MyMethod(object someObject)
{
   someObject = null;
}

object someObject = new object();
MyMethod(someObject);
Console.WriteLine(someObject == null); // Prints false

In the following case, actual pass by reference semantics are used:

void MyMethod(ref object someObject)
{
   someObject = null;
}

object someObject = new object();
MyMethod(ref someObject);
Console.WriteLine(someObject == null); // Prints true
jrista
You're right that it's important to note that "pass by reference" has a specific meaning - which is why your first statement of "everything that is a 'class' is passed by reference" is incorrect. The argument *isn't* passed by reference - it's passed by value, but that value is a reference. It's worth avoiding saying "pass by reference" when you mean "pass reference by value".
Jon Skeet
That is exactly what my answer was explaining....did you actually read it?
jrista
@jrista: Yes, I read it - which is why I ended up not downvoting. But your very first sentence is still misleading, and I believe it should be corrected.
Jon Skeet
That's fine for the first sentence - now for the third :) The second sentence also seems to be suggesting that *only* value types are passed by value. Personally I'd just ditch the whole of the first paragraph - the rest of your answer is much better.
Jon Skeet
A: 

The first thing to understand is that, in .Net, a variable can be either a value type (e.g int) or a reference type (a class). Value type variables point directly to a value in memory, whereas reference type variables point to a memory location.

By default, parameters are passed by value. However, remember that the 'value' of a reference type is actually its location in memory. So even though it's called passing by value, you are really passing a reference to a particular class.

James L