简体   繁体   中英

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? I know that equals must show true, because it compares values, but what about memory allocation for those strings? Why myString3 and myString4 are pointing to the same block of memory in the heap but myString and myString2 not?

C# compiler optimizes it so the same literals point to the same string instance

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.

I answer your question here:

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. 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:

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. 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.

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.

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.

I was previously under the impression that in earlier version of framework IsIntern was optional and let to developer. Looks like that isn't necessarily the case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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