Posted: 10/1/2010
Hello Folks,
I have read a blog on codeasp and modify the code -
Here is blog's code :-
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication { class Program { static void Main(string[] args) { int i, j; i = 23; j = i; Console.Write("Value Of i:{0}", i); Console.Write("\nValue Of j:{0}", j); i = 24; Console.Write("\nValue Of i:{0}", i); Console.Write("\nValue Of j:{0}", j); AgeClass ajit = new AgeClass(); ajit.SetAge(23); AgeClass sumit = ajit; //Sumit now equals ajit Console.Write("\nAge Of AJIT:{0} ", ajit.GetAge()); Console.WriteLine("\nAge Of Sumit:{0} ", sumit.GetAge()); ajit.SetAge(24); //change Ajit's age, what Sumit to ajit now? Console.Write("\nAge Of AJIT:{0} ", ajit.GetAge()); Console.WriteLine("\nAge Of Sumit:{0} ", sumit.GetAge()); Console.WriteLine(ajit == sumit); Console.ReadLine(); } } } public class AgeClass { private int age; public void SetAge(int years) { age = years; } public int GetAge() { return age; } }
As I know int is value type and sting or class is reference types. In above code value would be change in Age of Ajit 24 or Age of sumit 24. But if I am using string except class value won't change.
Here is modify code:-
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication { class Program { static void Main(string[] args) { int i, j; i = 23; j = i; Console.Write("Value Of i:{0}", i); Console.Write("\nValue Of j:{0}", j); i = 24; Console.Write("\nValue Of i:{0}", i); Console.Write("\nValue Of j:{0}", j); //string x, y; //x = "Mohit"; //y = x; //Console.WriteLine("X value is :"+ x); //Console.WriteLine("Y value is :" + y); //x = "Dahiya"; //Console.WriteLine("X value is :" + x); //Console.WriteLine("Y value is :" + y); Console.ReadLine(); } } } public class AgeClass { private int age; public void SetAge(int years) { age = years; } public int GetAge() { return age; } }
My question is - As string and class both are reference type.But in class case value of age will change and in string case Y value won't change.
Can anyone tell me why this is happening?
Thanks in advance.
In your first example object a and object b both points to same location so whatever is the changes it will affect both. In your second example
string a='xxx' a points to memory location say z
string b='fffff' b points to memory location say y
a=b both a and b points to same memory loc now
a='kkkkk' now a is pointing to diff location at this point b is still pointing to to old location so both of them will have different values.
mohit said: user="mohit kumar"]My question is - As string and class both are reference type.But in class case value of age will change and in string case Y value won't change.Can anyone tell me why this is happening?
user="mohit kumar"]
Hi Mohit,
Strings are immutable reference types and so they don't behave quite like other reference types. I believe have demonstrated you an example awhile back here:http://www.codeasp.net/forums/asp-net-topics/getting-started-general-asp-net/229/string
raghav_khunger said: user="Raghav Khunger"]In your first example object a and object b both points to same location so whatever is the changes it will affect both. In your second example string a='xxx' a points to memory location say zstring b='fffff' b points to memory location say ya=b both a and b points to same memory loc nowa='kkkkk' now a is pointing to diff location at this point b is still pointing to to old location so both of them will have different values.
user="Raghav Khunger"]
Thank you for reply.
Its mean value types save in Stack and references types save in Heap.But string case is different. String is references type and save in Heap. Like
string x = "first" Now "first" value(string value) save in stack and reference (address) save in Heap.
If I would change some value of x variable.Like
x = "second" Now x reference(address) is different save.And later garbage collector remove the first reference(address).
Am I right Raghav Sir?
Vinz said: user="Vincent Maverick Durano"] mohit said: user="mohit kumar"]My question is - As string and class both are reference type.But in class case value of age will change and in string case Y value won't change.Can anyone tell me why this is happening? Hi Mohit,Strings are immutable reference types and so they don't behave quite like other reference types. I believe have demonstrated you an example awhile back here:http://www.codeasp.net/forums/asp-net-topics/getting-started-general-asp-net/229/string
user="Vincent Maverick Durano"]
Thanks Vinz.
Can you please tell me what is immutable reference types ?
Mohit,
First of all don't get confused . First look the context. I can prove you even for strings in your first example you will get both of them equal. I have not tested but with the below code you will still get both of them having same value:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication { class Program { static void Main(string[] args) { int i, j; i = 23; j = i; Console.Write("Value Of i:{0}", i); Console.Write("\nValue Of j:{0}", j); i = 24; Console.Write("\nValue Of i:{0}", i); Console.Write("\nValue Of j:{0}", j); AgeClass ajit = new AgeClass(); ajit.SetAge("23"); AgeClass sumit = ajit; //Sumit now equals ajit Console.Write("\nAge Of AJIT:{0} ", ajit.GetAge()); Console.WriteLine("\nAge Of Sumit:{0} ", sumit.GetAge()); ajit.SetAge("24"); //change Ajit's age, what Sumit to ajit now? Console.Write("\nAge Of AJIT:{0} ", ajit.GetAge()); Console.WriteLine("\nAge Of Sumit:{0} ", sumit.GetAge()); Console.WriteLine(ajit == sumit); Console.ReadLine(); } } } public class AgeClass { private string age; public void SetAge(string years) { age = years; } public string GetAge() { return age; } }
mohit said: user="mohit kumar"]Can you please tell me what is immutable reference types ?
user="mohit kumar"]Can you please tell me what is immutable reference types ?
Immutable means that after the object has been instantiated, a string cannot change value (see the example i've provided in the link I posted).On the other hand Mutable type is a type whose instance data can be modified. The System.Text.StringBuilder class is an example of a mutable reference type. It contains members that can change the value of an instance of the class
raghav_khunger said: user="Raghav Khunger"]Mohit, First of all don't get confused . First look the context. I can prove you even for strings in your first example you will get both of them equal. I have not tested but with the below code you will still get both of them having same value: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication { class Program { static void Main(string[] args) { int i, j; i = 23; j = i; Console.Write("Value Of i:{0}", i); Console.Write("\nValue Of j:{0}", j); i = 24; Console.Write("\nValue Of i:{0}", i); Console.Write("\nValue Of j:{0}", j); AgeClass ajit = new AgeClass(); ajit.SetAge("23"); AgeClass sumit = ajit; //Sumit now equals ajit Console.Write("\nAge Of AJIT:{0} ", ajit.GetAge()); Console.WriteLine("\nAge Of Sumit:{0} ", sumit.GetAge()); ajit.SetAge("24"); //change Ajit's age, what Sumit to ajit now? Console.Write("\nAge Of AJIT:{0} ", ajit.GetAge()); Console.WriteLine("\nAge Of Sumit:{0} ", sumit.GetAge()); Console.WriteLine(ajit == sumit); Console.ReadLine(); } } } public class AgeClass { private string age; public void SetAge(string years) { age = years; } public string GetAge() { return age; } }
Yes sir that I have already tested. Both of them having same value in object case. but my question was why doesn't the same value in string case.And as u said above u can prove in string have same value also.
HOW?Can you plz do?
In the above example it is already proved since the referred object is same.
Vinz said: user="Vincent Maverick Durano"] mohit said: user="mohit kumar"]Can you please tell me what is immutable reference types ? Immutable means that after the object has been instantiated, a string cannot change value (see the example i've provided in the link I posted).On the other hand Mutable type is a type whose instance data can be modified. The System.Text.StringBuilder class is an example of a mutable reference type. It contains members that can change the value of an instance of the class
string string1 = "Hello";string1.Replace ("Hello", "Hi");string1 = string1.Replace ("Hello", "Hi");
I have run your code and understood about Immutable or mutable types.
Thanks Vinj
raghav_khunger said: user="Raghav Khunger"]In the above example it is already proved since the referred object is same.
Thank you so much Sir.I have got the answer
mohit said: user="mohit kumar"]string string1 = "Hello";string1.Replace ("Hello", "Hi");string1 = string1.Replace ("Hello", "Hi"); I have run your code and understood about Immutable or mutable types.Thanks Vinj
Cool. Glad it clears out your confusion. :D