简体   繁体   English

C#中的赋值和创建字符串实例有什么区别?

[英]What is the difference Between Assignment and Creating Instance of String in C#?

I have sample code. 我有示例代码。

var charMass = new char[] { 's', 't', 'r' };
string myString = new string(charMass);
string myString2 = new string(charMass);
string myString3 = "str";
string myString4 = "str";

bool bb1 = Object.ReferenceEquals(myString, myString2);
bool bb2 = Object.ReferenceEquals(myString, myString3);
bool bb3 = Object.ReferenceEquals(myString3, myString4);

Why bb1 and bb2 are false? 为什么bb1和bb2是假的? I know that equals must show true, because it compares values, but what about memory allocation for those strings? 我知道equals必须显示为true,因为它比较了值,但是那些字符串的内存分配呢? Why myString3 and myString4 are pointing to the same block of memory in the heap but myString and myString2 not? 为什么myString3和myString4指向堆中的同一块内存但myString和myString2却没有?

C# compiler optimizes it so the same literals point to the same string instance C#编译器优化它,因此相同的文字指向相同的字符串实例

MSDN : MSDN

The intern pool conserves string storage. 实习池保存字符串存储。 If you assign a literal string constant to several variables, each variable is set to reference the same constant in the intern pool instead of referencing several different instances of String that have identical values. 如果为多个变量分配文字字符串常量,则每个变量都设置为在实习池中引用相同的常量 ,而不是引用具有相同值的几个不同的String实例。

I answer your question here: 我在这里回答你的问题:

http://blogs.msdn.com/b/ericlippert/archive/2009/09/28/string-interning-and-string-empty.aspx . http://blogs.msdn.com/b/ericlippert/archive/2009/09/28/string-interning-and-string-empty.aspx

The short answer is: interning literal strings is cheap and easy and therefore is done by default. 简短的回答是:实习文字字符串便宜且容易 ,因此默认情况下完成。 Interning dynamically-allocated strings typically saves a small number of bytes at the cost of a huge amount of time and is therefore not worth bothering about . 实际动态分配的字符串通常以大量时间为代价节省少量字节,因此不值得打扰 If you want to force interning, you can do so yourself. 如果你想强制实习,你可以自己动手。

myString and myString2 can never be reference-equal to each other (or to any other string) because you explicitly called the string constructor, resulting in a new object being created each time. myStringmyString2永远不能引用 - 彼此相等(或任何其他字符串),因为您显式调用了字符串构造函数,导致每次都创建一个新对象。 Obviously this new object will not be reference-equal to any other already-existing object. 显然,这个新对象不会被引用 - 等于任何其他已存在的对象。

myString3 and myString4 are reference-equal because the compiler interns the strings : string values initialized with string literals at compile time end up being references to the same object at runtime: myString3myString4是引用相等的,因为编译器实现了字符串 :在编译时用字符串文字初始化的字符串值最终是在运行时对同一对象的引用:

The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. 公共语言运行库通过维护一个名为intern pool的表来保存字符串存储,该表包含对在程序中以编程方式声明或创建的每个唯一文字字符串的单个引用。 Consequently, an instance of a literal string with a particular value only exists once in the system. 因此,具有特定值的文字字符串实例仅在系统中存在一次。

For example, if you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable. 例如,如果将相同的文字字符串分配给多个变量,则运行时将从实习池中检索对文字字符串的相同引用,并将其分配给每个变量。

也许编译器以某种方式将"str"优化为单个文字 ,然后将其分配给每个变量,当然,字符串是指针,意味着它们都指向相同的地址。

This is based on the underlying implementation of String in the framework. 这是基于框架中String的底层实现。

http://msdn.microsoft.com/en-us/library/system.string.intern.aspx http://msdn.microsoft.com/en-us/library/system.string.intern.aspx

The way I see it is that for myString and myString2, you created those using char[] and there is not default lookup in Intern pool as string is being created. 我看到的方式是,对于myString和myString2,您使用char []创建了那些,并且在创建字符串时,Intern池中没有默认查找。

In case of myString3 and myString4, myString3 added value to InternPool and for myString4, you just got a reference as it was the sample block. 在myString3和myString4的情况下,myString3为InternPool和myString4增加了值,你只需要一个引用,因为它是样本块。

I was previously under the impression that in earlier version of framework IsIntern was optional and let to developer. 我之前的印象是在早期版本的框架中IsIntern是可选的,并且让开发人员。 Looks like that isn't necessarily the case 看起来不一定是这种情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM