I know that strings have variable length, therefore they need variable space in memory to be stored. When we define a string item in a struct, the struct‘s size would then be variable in length.
Older languages managed this by using fixed-length strings. However, 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.
How does C# handle variable-length strings in structs?
The string itself is not stored in the struct. Instead a reference to the string is stored in the struct, so the struct size never changes.
stringis not a value type; .NET strings are interned, which means that each unique string is stored in a look-up table in memory.