I have made some ref
keyword tests and there is one thing I can't understand:
static void Test(ref int a, ref int b)
{
Console.WriteLine(Int32.ReferenceEquals(a,b));
}
static void Main(string[] args)
{
int a = 4;
Test(ref a, ref a);
Console.ReadLine();
}
Why does this code display False
? I know that int
is a value type but here it should pass references to the same object.
Why does this code display
False
?
Because int a
and int b
are being boxed when you call object.ReferenceEquals
. Each integer is boxed inside an object
instance. Thus, you are actually comparing references between two boxed values, which clearly aren't equal.
You can easily see this if you look at the generated CIL for the method:
Test:
IL_0000: nop
IL_0001: ldarg.0 Load argument a
IL_0002: ldind.i4
IL_0003: box System.Int32
IL_0008: ldarg.1 Load argument b
IL_0009: ldind.i4
IL_000A: box System.Int32
IL_000F: call System.Object.ReferenceEquals
IL_0014: call System.Console.WriteLine
IL_0019: nop
IL_001A: ret
Checking for storage location equality can be achieved either by using verifiable CIL (such as in @leppie's answer) or by unsafe
code:
unsafe static void Main(string[] args)
{
int a = 4;
int b = 5;
Console.WriteLine(Test(ref a, ref a)); // True
Console.WriteLine(Test(ref a, ref b)); // False;
}
unsafe static bool Test(ref int a, ref int b)
{
fixed (int* refA = &a)
fixed (int* refB = &b)
{
return refA == refB;
}
}
This cannot be done directly in C#.
You can however implement it in verifiable CIL:
.method public hidebysig static bool Test<T>(!!T& a, !!T& b) cil managed
{
.maxstack 8
ldarg.0
ldarg.1
ceq
ret
}
Tests
int a = 4, b = 4, c = 5;
int* aa = &a; // unsafe needed for this
object o = a, p = o;
Console.WriteLine(Test(ref a, ref a)); // True
Console.WriteLine(Test(ref o, ref o)); // True
Console.WriteLine(Test(ref o, ref p)); // False
Console.WriteLine(Test(ref a, ref b)); // False
Console.WriteLine(Test(ref a, ref c)); // False
Console.WriteLine(Test(ref a, ref *aa)); // True
// all of the above works for fields, parameters and locals
Notes
This does not actually check for the same reference, but even more fine-grained in that it makes sure both are the same 'location' (or referenced from the same variable) too. This is while the 3rd line returns false
even though o == p
returns true
. The usefulness of this 'location' test is very limited though.
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