Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the difference between mutable and immutable String class?

Why Data always flows as string but not as number?
Posted

1) All strings in .NET are immutable. You never can change a content of any string. All string operations create a brand-new string. Keep it in mind when using string operations, especially highly misused concatenation ("+").

Compare:
C#
string myString = "Immutable";
char charI = myString[0]; //you can do it
//myString[0] = ' '; myString[1] = ' '; no way; it is immutable

//array of char can be considered as some kind of string; it is mutable:
char[] myText = new char['M', 'u', 't', 'a', 'b', 'l', 'e', ' ', ];
myText[7] = '!'; //you can do it! The result will be "Mutable!"


In you want to concatenate strings repeatedly, always use System.Text.StringBuilder. More generally, whenever you need some effects of mutable string data, use this class. This class is not a string though; it's a container of character data with operations which you can convert to string via object.ToString().

2) Not true. There is no "why". If flows how you define it.

—SA
 
Share this answer
 
v3
Comments
Mehdi Gholam 11-Sep-11 0:52am    
My 5!
Sergey Alexandrovich Kryukov 11-Sep-11 15:19pm    
Thank you, Mehdi.
--SA
djrocks0101 11-Sep-11 1:44am    
Thanks SAKryukov..My 5!
Sergey Alexandrovich Kryukov 11-Sep-11 15:20pm    
You are welcome and thank you.
Good luck, call again.
--SA
Uday P.Singh 11-Sep-11 3:01am    
my 5!
We are now that strings are reference type in .net. Due to this fact, they are allocated on heap and when in the scope their pointer come on to the stack. When we try and assign a new value to a string variable, we can get the new value assigned to our variable, but in the background, a new memory location is allocated and its reference is copied to out string variable making us feel that the value of the original one is changed. This feature is referred as immutable. Thus all string in .net are immutable.

Due to this behavior of string we should always use StringBuilder class present in System.IO.Text namespace where we thing we need lot of string manipulation. This class has lot of methods that we can use to manipulate the strings.

We can get our string using the following code
C#
StringBuilder sb=new StringBuilder();
//do some stuff like string manipulation here
string s=db.ToString();


The data in the backend flows mostly in form of binary or xml strings if it flows between processes but flows as given type if it is inproc call.

Hope this helps

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900