Unexpected Chinese Characters in StringBuilder Char array

149 Views Asked by At

This is more of a curiosity question. All our software & hardware on the network are UK based. I'm using the StringBuilder.AppendLine() method to build some basic CSV data for a download on a web app. I was investigating the price column in the SB Object to discover that quite a number of what appear to be Chinese characters are present in the Char array. I'm sure it might be some encoding issue as when I paste it out, I cannot see symbols that are pulled in from the database like the % sign and such. Still, it got me thinking as to why this might be happening or is it some clever implementation where a .NET can represent multiple English characters as a single Chinese one? Please see image attached.

enter image description here

To be clear, the data being populated in the SB from the DB is all English strings and ints/bigints etc/

Copying the data from the debugging table into Notepad++ shows a list like so:

  1. b
  2. l
  3. e
  4. t
  5. """"
  6. ","
  7. """"
  8. K
  9. i
  10. s
  11. q
  12. a
  13. l

The length of the array in the debugger is around 4k entries and when pasted in to Notepad, is around 2500 lines. The double quotes above are the only entries that don't look like a single char in the array when copied and pasted out. But I believe "" represents null for a char?

ADDED: As per the comment below, these characters do not display in ths CSV only the Visual Studio enumeration window.

Target Framework: net6.0 Version: 7.0.102

To access the Visual Studio enumeration window:

enter image description here

Hover over the variable and expand the line to get the image above. Click View to open the Char array.

UPDATE: Minimal Reproduceable Example:

var csvBuilder = new StringBuilder();
string test = "144039058,385124005,3932411000001100,Atropine 1% eye drops 5 ml,Atropine 1% eye drops 5 ml,TEST Pharmaceuticals Ltd,No,Eye drops,5,ml,122.27,24.45";
for(int i = 0; i < 8; i++)
    csvBuilder.AppendLine(test);

I choose 8 as these characters seem to be added around the 7th or 8th iteration: enter image description here

1

There are 1 best solutions below

0
Jon Skeet On

I looks like you're using the debugger to visualize a private field within StringBuilder (m_ChunkChars). That's going to be very implementation-specific; maybe StringBuilder has some weird sort of compression going on, or there it's an array which is only partially populated and you're looking beyond the populated part, to randomly-initialized data.

Basically if you have to use the debugger to drill into the private fields of a type you don't own, you shouldn't be surprised if sometimes the results are surprising. I would basically try to avoid using the IEnumerable Visualizer in this way.