CSharp - What is the difference between String and StringBuilder?

Introduction

Strings are not editable.

StringBuilder does not create a new instance for editing.

Demo

using System;
using System.Runtime.Serialization;//for ObjectIDGenerator
using System.Text;//for StringBuilder

class Program//w ww.ja  va2 s.c o  m
{
    static void Main(string[] args)
    {
        ObjectIDGenerator idGenerator = new ObjectIDGenerator();
        bool firstTime = new bool();

        string myString = "Hello World";
        Console.WriteLine("{0} Instance Id now : {1}", myString, idGenerator.GetId(myString, out firstTime));
        //creates new instance ID
        myString = myString + ",programmer";
        Console.WriteLine("{0} Instance Id now : {1}", myString, idGenerator.GetId(myString, out firstTime));
        StringBuilder myStringBuilder = new StringBuilder("Hello,Mr StringBuilder");

        Console.WriteLine("{0} Instance Id : {1}", myStringBuilder, idGenerator.GetId(myStringBuilder, out firstTime));
        //Do not create a new instance ID
        myStringBuilder = myStringBuilder.Replace("Hello", "Welcome");
        Console.WriteLine("{0} Instance Id : {1}", myStringBuilder, idGenerator.GetId(myStringBuilder, out firstTime));
        Console.ReadKey();
    }
}

Result