Make a reference to another string in C#

Go To


As far as I know a string in C# is a reference type.

So in the following code 'a' should be equal to "Hi", but it still keeps its value which is "Hello". Why?

string a = "Hello";
string b = a;
b = "Hi";
2012-04-04 05:23
by Nour Sabouny
Check this for details : - ZeNo 2012-04-04 05:27
@ZeNo: this has nothing to do with string immutability. It's normal reference type behaviour - porges 2012-04-04 05:28
Similar question: - SimpleVar 2012-04-04 05:29


A number of the answers point out that strings are immutable; though that is true, it is completely irrelevant to your question.

What is more relevant is that you are misunderstanding how references work with respect to variables. A reference is not a reference to a variable. Think of a reference as a piece of string. You start with this:


Then you say that "b = a", which means attach another piece of string to the same thing that a is attached to:


Then you say "now attach b to Hi"



You are thinking either that references work like this:


Then I say that b is another name for a:

a/b ----------------------Hello

Then I change b, which changes a, because they are two names for the same thing:

a/b ----------------------Hi

Or perhaps you are thinking that references work like this:


Then I say that b refers to a:

b -------------- a ----------------------Hello

Then I change b, which indirectly changes a:

b -------------- a ----------------------Hi

That is, you are expecting to make a reference to a variable, instead of a value. You can do that in C#, like this:

void M(ref int x)
    x = 1;
int y = 0;
M(ref y);

That means "for the duration of the call to M, x is another name for y". A change to x changes y because they are the same variable. Notice that the type of the variable need not be a reference type.

2012-04-04 05:49
by Eric Lippert


The line b = "Hi"; changes which string b references. a still references "Hello".

string a = "Hello";  // Set a to reference the string "Hello"
string b = a;        // Set b to reference the same string as a
b = "Hi";            // Set b to reference the string "Hi"
2012-04-04 05:27
by shf301


You are changing the reference b. Not a. The reference itself is copied while the object remains untouched. So b = "Hi" copies a reference to the "Hi" object into b. This does not affect a.

2012-04-04 05:28
by Esben Skov Pedersen


The concept of a reference type is the most confusing thing amongst OOP programmers.

Run the below code, and you will be surprised to see the answer:

Create a simple Book class with a property called Name and write the below code in the Main method of the application.

Book a = new Book() {Name = "book a"};
Book b = new Book() {Name = "book b"};

Book c = a; //line 3

Book a = b; //Line 4


And as no doubt you will expect the answer to be "book b" because of line 4. You think that as c is a and after that a became b which will also make c equals b.

Which is not the case!

Read the balloon anology at Ballon analogy for Reference type.

2012-04-04 06:09
by Dhananjay
actually, I already know that : - Nour Sabouny 2012-04-04 07:24
Ok, Replace Book with string in the example and check if you should have asked the same question or not - Dhananjay 2012-04-04 08:45
Ok, I didn't realize that this line (a="Hi") is like making the variable (a) refers to another object,I thought it is changing the object that the variable (a) is referring to - Nour Sabouny 2012-04-04 10:08



What you did, is to create two references ('a','b') to a string "Hello". With b = "Hi" you change 'b' to reference the string "Hi".

'a' will never change this way.

2012-04-04 05:31
by mo.