StringBuilder a = new StringBuilder();
a.Append("the test a");
String s1 = a.ToString();
String s2 = "the test a";
if (s2 == s1) //为真,因为S2,S1内容相等。String类的==与Equals的行为相同,判断两个字符串的内容是否相等
Console.WriteLine("== returns true");
if (Object.Equals(s2, s1)) //为真,String类重载了Equals,用于判断两个字符串的内容是否相等
{
Console.WriteLine("equals returns true");
}
if (Object.ReferenceEquals(s2, s1)) //为假,因为s2,s1并不是指向同一个对象
{
Console.WriteLine("ReferenceEquals returns true");
} |
|