积极答复者
关于String的ReferenceEquals

问题
-
问题:黑色的那行
static void Main(string[] args)
{
string x = "should it matter";
string y = "should it matter";
Console.WriteLine(x == y);//true
Console.WriteLine(object.ReferenceEquals(x, y));//true 字符串保留用string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
string b = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
Console.WriteLine(a == b); //true
Console.WriteLine(a.Equals(b));//true
Console.WriteLine(object.ReferenceEquals(a, b));//false 为什么?Console.ReadLine();
}
答案
-
-
刚看了下msdn,自己的理解是这样的,不知道对不对? 对于字符串a,没有被留用,因为赋值给他的不是一个常量! string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o' }); 在下面的 C# 示例中,值为“MyTest”的字符串 s1 已经留用,因为它在程序中是一个字符串常量。 String s1 = "MyTest"; http://msdn.microsoft.com/zh-cn/library/system.string.intern(VS.80).aspx
- 已建议为答案 Raymond TangModerator 2009年12月14日 1:30
- 已标记为答案 Riquel_DongModerator 2009年12月17日 3:12
全部回复
-
刚看了下msdn,自己的理解是这样的,不知道对不对? 对于字符串a,没有被留用,因为赋值给他的不是一个常量! string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o' }); 在下面的 C# 示例中,值为“MyTest”的字符串 s1 已经留用,因为它在程序中是一个字符串常量。 String s1 = "MyTest"; http://msdn.microsoft.com/zh-cn/library/system.string.intern(VS.80).aspx
- 已建议为答案 Raymond TangModerator 2009年12月14日 1:30
- 已标记为答案 Riquel_DongModerator 2009年12月17日 3:12
-