Why was String designed as a reference type instead of value type?
From the modeling perspective I would have modeled it as a value type since it represents something without identity. It doesn’t have distinguishing attributes. (E.g I can’t make any difference between one string “a” and another string “a”)
I know that I would have had serious performance problems having long strings stored on the stack. Probably it’s impossible, as strings get very long, because stack is limited in size.
If it weren’t for the performance why would you design System.String as a reference type?
(Assume any possible string is at most 16 bytes long)
Structs need to be fixed size. Think of a
string[], for example. The only way you could havestringas a value-type would be to store just the pointer. Which is essentially what we achieve by using a reference-type.Of course, it is also hugely beneficial that we don’t copy the string every time we assign it.