Originally Posted by
MisterT
This will not work so long as your dealing with generic types.
Please define what you mean by "this will not work," because this appears to work just fine:
Code:
class A : IEquatable<A>
{
int m_int;
public A(int a) { m_int = a; }
public override bool Equals(object obj)
{
A other = obj as A;
if (other == null)
return false;
return Equals(other);
}
public bool Equals(A other)
{
return m_int == other.m_int;
}
}
static bool someMethod<T>(T a, T b)
{
if (a.Equals(b))
{
Console.WriteLine("Hooray!");
return true;
}
Console.WriteLine("Whoops!");
return false;
}
[STAThread]
static void Main(string[] args)
{
A a = new A(3);
A b = new A(4);
A a2 = new A(3);
someMethod(a, b);
someMethod(a, a2);
someMethod(a2, b);
Console.WriteLine("Finished");
Console.ReadLine();
}
Output:
Code:
Whoops!
Hooray!
Whoops!
Finished