There is no difference between them. You are confused by overriding Equals (which is not called in any of the implementations) with overload == (which will not be relevant in any fragment, since overloading is performed during compilation, and the compiler does not know enough about T to use any specific overload) .
Just to show what I mean:
static void ThrowIfFoo<T>(this T argument, string name) where T : class { if (argument == "foo") { throw new Exception("You passed in foo!"); } }
Testing with:
"foo".ThrowIfFoo(); // Throws string x = "f"; x += "oo"; // Ensure it actually a different reference x.ThrowIfFoo(); // Doesn't throw
ThrowIfFoo does not know that T will be a string - because it depends on the calling code - and overload resolution is only performed when ThrowIfFoo compiled. Therefore, it uses the operator ==(object, object) , and not ==(string, string) .
In other words, it is like this:
object foo1 = "foo"; string tmp = "f"; object foo2 = tmp + "oo"; Console.WriteLine(foo1.Equals(foo2));
On the last line, the compiler knows that it can use the == overload because both operands have string time types.
source share