string first="Example";
string second="Example";
这两个字符串具有相同的哈希码,并且两个引用也是相同的。我的问题是:这些字符串变量在内存位置上是否只有一份数据副本?
string third="Example";
static string fourth="Example";
const string fifth="Example";
上述两个集合之间有什么区别吗?
string first="Example";
string second="Example";
这两个字符串具有相同的哈希码,并且两个引用也是相同的。我的问题是:这些字符串变量在内存位置上是否只有一份数据副本?
string third="Example";
static string fourth="Example";
const string fifth="Example";
test2
和test3
是同一个引用 - 尽管test.ToString()
返回的中间字符串是分开的;当这个字符串通过string.Intern
时,你会得到与ldstr
获得的相同的内部版本。首先,test
不是一个string
,因此不能是同一个引用。 - Marc Gravellstring
,已经定义了一些运算符;由于 string
类型是内置的,因此您无法向其添加运算符 - 但是您拥有的其他类型可以声明涉及字符串的转换、相等性等运算符。 - Marc Gravell
new string("Example")
时,您将创建一个新引用指向新实例。 - MakePeaceGreatAgain