I wonder what the difference is between the following methods with regards to how the object parameter is referenced:
public void DoSomething(object parameter){}
and
public void DoSomething(ref object parameter){}
Should I use ref object parameter
in cases where I want to change the reference to the object
not override the object in the same reference?
public void DoSomething(object parameter)
{
parameter = new Object(); // original object from the callee would be unaffected.
}
public void DoSomething(ref object parameter)
{
parameter = new Object(); // original object would be a new object
}
See the article: Parameter Passing in C# by Jon Skeet
In C#, Reference type object's address is passed by value, when the ref
keyword is used then the original object can be assigned a new object or null, without ref
keyword that is not possible.
Consider the following example:
class Program
{
static void Main(string[] args)
{
Object obj1 = new object();
obj1 = "Something";
DoSomething(obj1);
Console.WriteLine(obj1);
DoSomethingCreateNew(ref obj1);
Console.WriteLine(obj1);
DoSomethingAssignNull(ref obj1);
Console.WriteLine(obj1 == null);
Console.ReadLine();
}
public static void DoSomething(object parameter)
{
parameter = new Object(); // original object from the callee would be unaffected.
}
public static void DoSomethingCreateNew(ref object parameter)
{
parameter = new Object(); // original object would be a new object
}
public static void DoSomethingAssignNull(ref object parameter)
{
parameter = null; // original object would be a null
}
}
Output would be:
Something
System.Object
True
passing a variable by ref allows the function to repoint that variable to another object, or indeed null: e.g.
object parameter = new object();
FailedChangeRef(parameter); // parameter still points to the same object
ChangeRef(ref parameter); // parameter now points to null
public void FailedChangeRef(object parameter)
{
parameter = null; // this has no effect on the calling variable
}
public void ChangeRef(ref object parameter)
{
parameter = null;
}
Argument Passing ByVal:
Describes passing arguments by value, which means the procedure cannot modify the variable itself.
Argument Passing ByRef:
Describes passing arguments by reference, which means the procedure can modify the variable itself.
In C#, the default mechanism of method arguments is Pass by Value. Hence if your are declaring a method like,
public void DoSomething(object parameter){} // Pass by value
So, a new copy of the object is created, so the changes on the parameter will not affect the original object passed in.
But, when u pass the parameter by ref, it is Pass by Reference
public void DoSomething(ref object parameter) // Pass by reference
Now, your are operating on the address on the originally passed object. Hence the changes u are making on the parameter inside the method will affect the original object.
When you see ref object
that means, argument has to be object
type.
You can read in documentation:
When a formal parameter is a reference parameter, the corresponding
argument in a method invocation must consist of the keyword ref
followed by a variable-reference (§5.3.3) of the same type as the
formal parameter. A variable must be definitely assigned before it can
be passed as a reference parameter.