class Foo
{
public int A { get; set; }
}
class Program
{
static void Main(string[] args)
{
var f = new Foo();
var ff = f;
Console.WriteLine(f.GetHashCode());
Console.WriteLine(ff.GetHashCode());
FooFoo(ref f);
BarBar(f);
}
private static void BarBar(Foo f)
{
Console.WriteLine(f.GetHashCode());
}
private static void FooFoo(ref Foo f)
{
Console.WriteLine(f.GetHashCode());
}
}
OUTPUT:
58225482
58225482
58225482
58225482
What is the difference between FooFoo and BarBar?
No effective difference in this case.
The point of
refis not to pass a reference to an object, it is to pass a reference to a variable.Consider the following change:
In this case, the
FooFoo‘s change will also change thefvariable in theMainmethod, but theBarBarmethod is only changing a local variable.All reference types are passed as reference types in any case, you cannot change that. The only point to
refis to allow the called method to change the variable it is being passed.