The compiler is typically pretty smart when it comes to string literals. Take a look at your code with Reflector and the debugger disassembler.
static void Main(string[] args)
{
string a = "A" + "B" + "C" + "D" + "E" + "F" + "G";
Console.WriteLine(a);
}
//reflector:
private static void Main(string[] args)
{
string text1 = "ABCDEFG";
Console.WriteLine(text1);
}
//but it not smarter than your application:
static void Main(string[] args)
{
string a = "A";
string b = "B";
string c = "C";
string d = a + b + c;
Console.WriteLine(a);
}
//reflector:
private static void Main(string[] args)
{
string text1 = "A";
string text2 = "B";
string text3 = "C";
string text4 = text1 + text2 + text3;
Console.WriteLine(text1);
}
//and now for a slightly different question and answer:
static void Main(string[] args)
{
string a = "A" + "B" + "C" + "D" + "E" + "F" + "G";
string b = "ABCDEFG";
}
take a look at the debug disassembly and you will see that both a and b are initialized with the same literal:
string a = "A" + "B" + "C" + "D" + "E" + "F" + "G";
00000029 mov eax,dword ptr ds:[0227307Ch]
0000002f mov edi,eax
string b = "ABCDEFG";
00000031 mov eax,dword ptr ds:[0227307Ch]
00000037 mov esi,eax