R
Ricardods1006
Could someone please clarify a small point of confusion for me please.
MSDN excerp ...
When applying the where T : class constraint, avoid the == and != operators
on the type parameter because these operators will test for reference
identity only, not for value equality. This is the case even if these
operators are overloaded in a type that is used as an argument. The following
code illustrates this point; the output is false even though the String class
overloads the == operator.
public static void OpTest<T>(T s, T t) where T : class
{
System.Console.WriteLine(s == t);
}
static void Main()
{
string s1 = "target";
System.Text.StringBuilder sb = new System.Text.StringBuilder("target");
string s2 = sb.ToString();
OpTest<string>(s1, s2);
}
.......................
Except if lines
System.Text.StringBuilder sb = new System.Text.StringBuilder("target");
string s2 = sb.ToString();
are changed to
string s2 = "target";
then the output is true.
I can't understand why this is, are these no longer two different objects if
I do not use StringBuilder
Many thanks
MSDN excerp ...
When applying the where T : class constraint, avoid the == and != operators
on the type parameter because these operators will test for reference
identity only, not for value equality. This is the case even if these
operators are overloaded in a type that is used as an argument. The following
code illustrates this point; the output is false even though the String class
overloads the == operator.
public static void OpTest<T>(T s, T t) where T : class
{
System.Console.WriteLine(s == t);
}
static void Main()
{
string s1 = "target";
System.Text.StringBuilder sb = new System.Text.StringBuilder("target");
string s2 = sb.ToString();
OpTest<string>(s1, s2);
}
.......................
Except if lines
System.Text.StringBuilder sb = new System.Text.StringBuilder("target");
string s2 = sb.ToString();
are changed to
string s2 = "target";
then the output is true.
I can't understand why this is, are these no longer two different objects if
I do not use StringBuilder
Many thanks