If the string operation doesn't change the value of string, will that end up in creation of a new instance?
For example,
string str = "foo";
str += "";
I know the difference between string and StringBuilder in C#.
No, a new instance will be created only when the string operation changes the value in the string variable.
It can be proved using the ObjectIDGenerator class. It's worth reading this complete article for proof.
using System;
using System.Text;
using System.Runtime.Serialization;
namespace StringVsStringBuilder
{
class Program
{
static void Main(string[] args)
{
ObjectIDGenerator idGenerator = new ObjectIDGenerator();
bool blStatus = new bool();
// blStatus indicates whether the instance is new or not
string str = "Fashion Fades,Style Remains Same";
Console.WriteLine("Initial state");
Console.WriteLine("str = {0}", str);
Console.WriteLine("Instance id: {0}", idGenerator.GetId(str, out blStatus));
Console.WriteLine("This is new instance: {0}", blStatus);
// A series of operations that won't change value of str
str += "";
// Try to replace character 'x' which is not present in str so no change
str = str.Replace('x', 'Q');
// Trim removes whitespaces from both ends so no change
str = str.Trim();
str = str.Trim();
Console.WriteLine("\nFinal state");
Console.WriteLine("str = {0}", str);
Console.WriteLine("Instance id: {0}", idGenerator.GetId(str, out blStatus));
Console.WriteLine("This is new instance: {0}", blStatus);
Console.ReadKey();
}
}
}