0

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?

Oliver Nicholls
  • 1,342
  • 12
  • 24
  • 1
    Particularly the top answer - string *literals* are interned anyway. – Damien_The_Unbeliever Feb 05 '19 at 13:39
  • 1
    "When two or more string literals that are equivalent according to the string equality operator, appear in the same assembly, these string literals refer to the same string instance." [ECMA-334, pg. 26](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-334.pdf). In other words, `","` is `","` is `","`, no matter how often assigned. – Jeroen Mostert Feb 05 '19 at 13:39
  • You might also want to think about using string.Join for this particular case rather than trying to construct a string with proper separations yourself. Then you don't need to worry about this at all – emagers Feb 05 '19 at 13:40
  • Also, you *can* obtain a pointer to a string, or rather its contents, with `fixed (char* p = ",") { Console.WriteLine((IntPtr) p); }` (requires an `unsafe` context). This is not the same as a pointer to a `string` structure, but you almost never deal with that anyway. – Jeroen Mostert Feb 05 '19 at 13:41

0 Answers0