3 ms·
It's the same in C#, == is used for reference comparison, you use it when you want to know whether two objects are the same object, whereas you would typically
by heresy 16y ago
It's the same in C#, == is used for reference comparison, you use it when you want to know whether two objects are the same object, whereas you would typically use .Equals() for value comparison.
There is no wrapper class for "primitives" in C# though, to box it, you cast it to System.Object, and that allocates an object instance for you on the heap. To unbox, cast it to System.Int32.
'int' in C# is just an alias for System.Int32 though, so I guess there is no analogue to the Java situation in C#. You'd either be comparing System.Object references, or System.Int32 value types, not some hybrid.
Example:
using System;
class Boxing
{
static void Main(string[] args)
{
int i1 = 5;
object o1 = 5;
object o2 = 5;
int i2 = (int)o2;
Console.WriteLine("o1 == o2: {0}", o1 == o2);
Console.WriteLine("i1.Equals(o1): {0}", i1.Equals(o1));
Console.WriteLine("i2: {0}", i2);
}
}
Prints:
o1 == o2: False
i1.Equals(o1): True
i2: 5