I have made some ref
keyword tests and there is one think I can\'t understand:
static void Test(ref int a, ref int b)
{
Console.WriteLine(In
I know, that int is a value type but here it should pass references to the same object.
Yes, the reference passed to the method are the same, but they are boxed (converted to object/reference type) in the ReferenceEquals
method.
That is why the result of your test returns false, since you are comparing references of two different objects, due to boxing.
See: Object.ReferenceEquals Method
When comparing value types. If
objA
andobjB
are value types, they are boxed before they are passed to theReferenceEquals
method. This means that if bothobjA
andobjB
represent the same instance of a value type, theReferenceEquals
method nevertheless returns false