Такой вот код:
public class Point
{
public int X { get; set; }
public int Y { get; set; }
public static bool operator ==(Point op1, Point op2)
{
Console.WriteLine("Point.operator==");
return op1.X == op2.X && op1.Y == op2.Y;
}
public static bool operator !=(Point op1, Point op2)
{
return !(op1 == op2);
}
}
public class Test
{
public void Main()
{
Point A = new Point();
A.X = 10;
A.Y = 11;
Point copyA = new Point();
copyA.X = 10;
copyA.Y = 11;
Point B = new Point();
B.X = 15;
B.Y = 12;
Console.WriteLine((A == copyA).ToString().ToUpper()); //true - используется Point.operator==
Console.WriteLine((A == B).ToString().ToUpper()); //false - используется Point.operator==
GenericTest<Point>(A, copyA, B);
}
static void GenericTest<T>(T A, T copyA, T B)
where T : class
{
Console.WriteLine((A == copyA).ToString().ToUpper()); //false
Console.WriteLine((A == B).ToString().ToUpper()); //false
}
static void NonGenericTest(Point A, Point copyA, Point B)
{
Console.WriteLine((A == copyA).ToString().ToUpper()); //true - используется Point.operator==
Console.WriteLine((A == B).ToString().ToUpper()); //false - используется Point.operator==
}
}
Вопрос в том почему, для одних и тех-же значения вызывается в одном случае вызывается Point.operator==, а в другом object.operator== !!!
Почему для метода с параметром типа, не вызывается Point.operator==