I looked at the similar questions and read some articles. THis article has some pictures which makes it clear.
SomeObject so = new SomeObject();
somefunction(so);
Console.write(so.x); // will print 1
SomeObject so1 = new SomeObject();
somefunctionByRef(so1);
Console.write(so1.x); // will print 1
static void somefunction(SomeObject so)
{
so.x = 1;
}
public void somefunctionByRef(ref SomeObject so)
{
so.x = 1;
}
Both of the methods have the same effect on this reference type. so why choose ref
keyword for reference types?
is it a bad practice (possibly false) to use somefunction(SomeObject so) and modify the object inside the method and expect the changes without using the ref keyword?