Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Since strings are immutable, do variables with identical string values point to the same string object?

Tags:

string

c#

.net

a)

        string s  = "value";
        string s1 = "value";

Do s and s1 reference variables point to same string object ( I’m assuming this due to the fact that strings are immutable )?

b) I realize equality operators ( ==, > etc ) have been redefined to compare the values of string objects, but is the same true when comparing two strings using static methods Object.Equals() and Object.ReferenceEquals()?

thanx

like image 585
AspOnMyNet Avatar asked Feb 09 '10 19:02

AspOnMyNet


People also ask

What does immutable mean and how are values held in string objects changed?

There is a term called immutable, which means the state of an object can't be changed after is has been created. A string is an immutable type. The statement that a string is immutable means that, once created, it is not altered by changing the value assigned to it.

What does a string is immutable mean?

Immutable means that which cannot be changed or modified. So when you assign a value to a string, this value is created from scratch as opposed to being replaced. So everytime a new value is assigned to the same string, a copy is created. So in reality, you are never changing the original value.

Is string mutable or immutable?

String is an example of an immutable type. A String object always represents the same string. StringBuilder is an example of a mutable type. It has methods to delete parts of the string, insert or replace characters, etc.

Why are strings called immutable data type?

The String is immutable, so its value cannot be changed. If the String doesn't remain immutable, any hacker can cause a security issue in the application by changing the reference value. The String is safe for multithreading because of its immutableness. Different threads can access a single “String instance”.


3 Answers

No, not all strings with the same value are the same object reference.

Strings generated by the compiler will all be Interned and be the same reference. Strings generated at runtime are not interned by default and will be different references.

var s1 = "abc";
var s2 = "abc";
var s3 = String.Join("", new[] {"a", "b", "c"});
var s4 = string.Intern(s3); 
Console.WriteLine(ReferenceEquals(s1, s2)); // Returns True
Console.WriteLine(ReferenceEquals(s1, s3)); // Returns False
Console.WriteLine(s1 == s3); // Returns True
Console.WriteLine(ReferenceEquals(s1, s4)); // Returns True

Note the line above where you can force a string to be interned using String.Intern(string) which then allows you to use object equality instead of string equality for some checks, which is much faster. One example where this is very commonly used is inside the generated XML serializer code along with the name table.

like image 132
Samuel Neff Avatar answered Oct 02 '22 13:10

Samuel Neff


Yes, these will point to the same string, because they're both defined as string literals. If you create a string programatically, you'd have to manually intern the string.

This is because the .NET framework interns the string literals in your program into the intern pool. You can use String.Intern to retrieve this reference, or to manually intern your own, runtime-generated string.

For more details, refer to the docs for Intern:

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

like image 28
Reed Copsey Avatar answered Oct 02 '22 13:10

Reed Copsey


With the current CLR identical literal strings do point to the same actual objects. The process is called interning and applies to all compile time strings.

Strings created at runtime are not interned per default, but can be added to the interned collection by calling string.Intern.

Please see my answer for this question for a detailed explanation of how .NET strings are stored.

like image 36
Brian Rasmussen Avatar answered Oct 02 '22 13:10

Brian Rasmussen