8
제네릭에 문제가 있습니다. 제네릭에서 적은 수의 사업자를 사용하려고하면 전화가 끊기지 않습니다. 그러나 그것은 Equals 메서드와 함께 작동합니다.제네릭 클래스 연산자가 작동하지 않습니다
public class Test
{
public int i;
static public Boolean operator ==(Test obj1, Test obj2)
{
Console.WriteLine("operator ==");
return obj1.i == obj2.i;
}
static public Boolean operator !=(Test obj1, Test obj2)
{
Console.WriteLine("operator !=");
return obj1.i != obj2.i;
}
public override bool Equals(object obj)
{
Console.WriteLine("operator equals");
return this == (Test)obj;
}
public override int GetHashCode()
{
Console.WriteLine("HashCode");
return 5;
}
}
그리고 클래스 검사기 :
public class Checker
{
public Boolean TestGeneric<T>(T Left, T Right) where T : class
{
return Left == Right; //not work override operators
return Left.Equals(Right); //work fine
}
}
작은 테스트 :
Test left = new Test() { i = 4 };
Test right = new Test() { i = 4 };
var checker = new Checker();
Console.WriteLine(checker.TestGeneric<Test>(left, right));
Console.ReadKey();
내가 일반에서 클래스 테스트 덜 연산자를 사용할 수있는 방법 즉, 몇 가지 테스트 클래스입니까?
자신이하는 일을 모르는 경우 equals를 재정의하지 마십시오. 그렇다면 두 번 생각해보십시오! –
'return 5;'는 가장 큰 해시 코드가 아닙니다. – Jodrell
예. 대신 666을 사용해야합니다. –