D
DamienS
In the interests of me saving hair, can someone please explain to me
what's going on below? Why doesn't == work in comparing two int's when
cast as objects? They're the same type.
Note that it worked for strings.
Thanks in advance,
Damien
---------------
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
test();
}
static public void test()
{
object o1 = 5;
object o2 = 5;
Console.WriteLine(o1==o2); // returns false - why is this?
Console.WriteLine(o1.Equals(o2)); // returns true
// Check they're the same type
Console.WriteLine(o1.GetType().FullName); // Returns
System.Int32
Console.WriteLine(o2.GetType().FullName); // Returns
System.Int32
// try specifically casting
Console.WriteLine(Convert.ToInt32(o1) ==
Convert.ToInt32(o2)); // returns true
// How about with strings?
object o3 = "hello";
object o4 = "hello";
Console.WriteLine(o3 == o4); // returns true
Console.WriteLine(o3.Equals(o4)); // returns true
Console.ReadLine();
}
}
}
what's going on below? Why doesn't == work in comparing two int's when
cast as objects? They're the same type.
Note that it worked for strings.
Thanks in advance,
Damien
---------------
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
test();
}
static public void test()
{
object o1 = 5;
object o2 = 5;
Console.WriteLine(o1==o2); // returns false - why is this?
Console.WriteLine(o1.Equals(o2)); // returns true
// Check they're the same type
Console.WriteLine(o1.GetType().FullName); // Returns
System.Int32
Console.WriteLine(o2.GetType().FullName); // Returns
System.Int32
// try specifically casting
Console.WriteLine(Convert.ToInt32(o1) ==
Convert.ToInt32(o2)); // returns true
// How about with strings?
object o3 = "hello";
object o4 = "hello";
Console.WriteLine(o3 == o4); // returns true
Console.WriteLine(o3.Equals(o4)); // returns true
Console.ReadLine();
}
}
}