I know a string is immutable and cannot be redefined, but in this foreach loop the string is changed multiple times by adding elements of an array.
var stringOfNames = "";
var arrayOfNames = new string[5] { "jack", "marry", "joe", "jimmy", "bud" };
foreach (var item in arrayOfNames)
{
stringOfNames += item;
Console.WriteLine(stringOfNames);
}
Expected:
An error stating "Variable is already defined in this scope."
Actual:
The string is changed by adding the other names.
Also, what's the difference between these two:
1)
var a = "something";
var a = "something else";
2)
var a = "something";
a+= "asdf";
Why does the second option work?