Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ref" keyword and reference types [duplicate]

someone in my team stumbled upon a peculiar use of the ref keyword on a reference type

class A { /* ... */ } 

class B
{    
    public void DoSomething(ref A myObject)
    {
       // ...
    }
}

Is there any reason someone sane would do such a thing? I can't find a use for this in C#

like image 855
Luk Avatar asked Jan 11 '11 09:01

Luk


1 Answers

Only if they want to change the reference to the object passed in as myObject to a different one.

public void DoSomething(ref A myObject)
{
   myObject = new A(); // The object in the calling function is now the new one 
}

Chances are this is not what they want to do and ref is not needed.

like image 133
Oded Avatar answered Sep 30 '22 18:09

Oded