I'm in the middle of a code review and I came across some code (C#.NET) that built up a JSON string using a StringBuilder. The JSON string contains an array and therefore uses commas to separate the data. It looked something like this:
StringBuilder json = new StringBuilder("{[");
string comma = string.Empty;
foreach (DataRow row in rows)
{
json.Append(comma);
json.Append(row["Something"].ToString());
comma = ",";
}
json.Append("]}");
So the first "comma" appends nothing, since it's the beginning of the array, then every time after that an actual comma is appended. Makes sense.
The problem I had with this was with this line:
string comma = string.Empty;
Since a string is immutable, every time you do this:
comma = ",";
...memory is reallocated, even though you're not changing anything. I thought it would make more sense to use a char instead of a string, and initially give it the value of an empty space, since this wouldn't hurt the JSON.
But then I thought that .NET may actually realise that a string is being assigned the same value, and forego any superfluous memory allocation. At first I tried writing some unsafe code, just so I could see if the memory address does, in fact, change in this scenario, but since strings are managed types I can't create pointers to them.
I've looked around in blog posts and on StackOverflow, but I've not been able to find an answer, and I don't know how I could test this myself. So my question is:
Will .NET forego memory allocation on a string, if the string is being assigned the same value that it already contains? Also, is there a way of confirming this from within a .NET project?