31 January 2013

String and String Builder in C#

  1. System.String is immutable.(Non updatable) 
  2. System.StringBuilder is mutable (Updatable) 
  3. Using StringBuilder, various string operations can be performed in most effective manner.
Example 
class Program 

static void Main(string[] args) 

//for example: 
string str = "hello"; //Creates a new object when we concatenate any other words along with str variable it does not actually modify the str variable, instead it creates a whole new string. 
str = str + " to all"; 
Console.WriteLine(str); 
StringBuilder s = new StringBuilder("Hi"); 
s.Append(" To All"); 
Console.WriteLine(s); 
Console.Read(); 

}