Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference Between String or System.String and System.Text.StringBuilder

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 731
    Comment on it

    The difference between System.Text.StringBuilder and string or System.String

    System.String and string keywords are the same, and you can use whichever naming convention you prefer. Essentially, string keyword is an alias for System.String class. String objects are immutable (i.e. they cannot be changed after they have been created) whereas StringBuilder is mutable(i.e. they can be changed after they have been created).

    All of the String methods and C# operator that seems to modify a string actually return the results in a new string object. StringBuilder offers better performance over the System.String or string because it is mutable.

     

    Example

    string or System.String - In this example, string str1 and str2 are concatenated by "+=" operator to combine the contents in a single string.

    string str1 = "First Name ";
    string str2 = "Last Name";
    
    // Concatenate str1 and str2. This actually creates a new string object 
    // and stores it in str1, releasing the reference to the original object.
    str1 += str2
    System.Console.WriteLine(str1);
    
    // Output: First Name Last Name

     

    System.Text.StringBuilder - In this example, I am creating an object of StringBuilder class and modifying the value of it.

    StringBuilder sb = new StringBuilder();
    sb.Append("First Name");
    sb.Append(" Last Name");
    
    System.Console.WriteLine(sb.ToString());
    // Output: First Name Last Name

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: