138.
public static void OpTest<T>(T s, T t) where T : class
{
System.Console.WriteLine(s == t);
}
public static void Main()
{
string s1 = "foo";
System.Text.StringBuilder sb = new System.Text.StringBuilder("foo");
string s2 = sb.ToString();
OpTest<string>(s1, s2);
}
Output will be?
View Description
The reason for this behavior is that, at compile time, the compiler only knows that T is a reference type, and therefore must use the default operators that are valid for all reference types. If you must test for value equality, the recommended way is to also apply the where T : IComparable constraint and implement that interface in any class that will be used to construct the generic class.