h.nodehi h.nodehi - 2 months ago 21
C# Question

How does C# manage strings in structs?

Sorry for bad English.

You know that strings have variable length, therefore they need variable space on memory to be stored. When we define a string item in a struct, struct's size becomes variable in result.

Older languages managed this by using fixed-length strings. Apparently there is no way to define fixed-length strings in C#, and C# manages normal strings in structs pretty good.

This becomes more weird when we define an array of such structs, or simply an array of strings. As result of any change (decrease/increase) in length of one string, all forward structs must be shifted.

Is this something which happens internally?


A reference to the string (which is not a value type) is stored in the struct, so the struct size never changes.