Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between 2 methods with ref object par and without?

Tags:

c#

ref

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?

like image 635
Harry89pl Avatar asked May 10 '13 08:05

Harry89pl


People also ask

What is the difference between ref and out parameters in C# with example?

ref keyword is used when a called method has to update the passed parameter. out keyword is used when a called method has to update multiple parameter passed. ref keyword is used to pass data in bi-directional way. out keyword is used to get data in uni-directional way.

What is the difference between the ref and out keywords Linkedin?

What is the difference between the ref and out keywords? Variables passed to out specify that the parameter is an output parameter, while ref specifies that a variable may be passed to a function without being initialized.

What is use of ref in C#?

Ref and out keywords in C# are used to pass arguments within a method or function. Both indicate that an argument/parameter is passed by reference. By default parameters are passed to a method by value. By using these keywords (ref and out) we can pass a parameter by reference.


1 Answers

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
like image 80
Habib Avatar answered Oct 03 '22 16:10

Habib