Passing a ref type in a property, does C# pass by value or ref
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 p开发者_JAVA技巧arent form is better.
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.
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
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.
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
精彩评论