I have a problem with a common one. When I try to use fewer operators in the generic, their call does not occur. But it works with the Equals method. This is some test class:
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; } }
And the Checker class:
public class Checker { public Boolean TestGeneric<T>(T Left, T Right) where T : class { return Left == Right;
Small testing:
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();
How can I use fewer statements in the Test class from the general?
operators generics c #
user3205810
source share