Reputation: 313
Objects are reference types; so this code will print out Angelo.
Object o = new Object();
o.Name = "Michael";
Object p = o;
o.Name = "Angelo";
Console.WriteLine(p);
I read that string is a reference type but behaves much like a value type(like int).
int x = 10;
int y = x;
x = 20;
Console.WriteLine(y);
The code above will print 10 because y copies the actual value in the variable x. Now for the string:
string s = "Hello";
string t = s;
s = "Hi";
Console.WriteLine(t);
Now the code above for string will print "Hello". My question is this; since string is a reference type, does string s hold a reference to the object Hello when you declare it?and string t also holds a reference to that object?and the reason why Hi wasn't printed out was because of Immutability?
Or does a string when declared hold the actual value of an object much like value types?like in the second code example.
I want to have a firm grasp of the basics and I am thankful for everyones' patience with the silly questions I post here :)
Upvotes: 2
Views: 269
Reputation: 1620
You can declare and initialize strings in various ways, as
// Declare without initializing.
string message1;
// Initialize to null.
string message2 = null;
// Initialize as an empty string.
// Use the Empty constant instead of the literal "".
string message3 = System.String.Empty;
The += operator creates a new string that contains the combined contents. That new object is assigned to the variable s1, and the original object that was assigned to s1 is released for garbage collection because no other variable holds a reference to it.
string s1 = "A string is more ";
string s2 = "than the sum of its chars.";
// Concatenate s1 and s2. This actually creates a new
// string object and stores it in s1, releasing the
// reference to the original object.
s1 += s2;
System.Console.WriteLine(s1);
// Output: A string is more than the sum of its chars.
have a look on this link , may solve your issue; http://msdn.microsoft.com/en-IN/library/ms228362.aspx
Upvotes: 1
Reputation: 101732
and the reason why Hi wasn't printed out was because of Immutability?
No, you are not doing the same thing. If you do:
Object p = o;
p = new Object();
o.Name = "Angelo";
Console.WriteLine(p);
This obviously will not affect the p
. So this has nothing to do with immutability. When you assign a new reference, you are throwing away the old one.When you say:
s = "Hi";
It creates a new string
object and store it's reference in s
.This is default behaviour.
The only exception to this is when you use ref
keyword with a reference type parameter it changes it's reference. For example:
void Foo(ref string x)
{
x = "Hi!";
}
string s = "Hello";
Foo(ref s);
// now s is 'Hi!'
Note that if you could change a property of string
, you could observe the same behaviour with strings
.
Upvotes: 1
Reputation: 98868
Let me try to explain line by line what is going on here;
First of all, string
is a reference type and reference types have 2 things. An object and a reference to that object.
string s = "Hello";
In this line, you have a "Hello"
object and a reference to that object called s
.
string t = s;
In this line, you have a reference called t
and it refers the same object (which is "Hello"
) that s
refers.
s = "Hi";
In this line, you create a new object called "Hi"
and your s
reference refers this object now. It doesn't refer the "Hello"
object anymore.
Console.WriteLine(t);
Since t
still refers to "Hello"
object, this string will be printed. Changing the object that s
refers doesn't effect it.
Upvotes: 3