Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are strings really in .NET?

Tags:

string

.net

Is a string actually a character array (is-a), or does it have a character array as an internal store (has-a), or is it's own object which can expose itself as a with an array of characters?

I am more inclined to say it is it's own object, but then why are we so inclined to always say "A string is an array of characters..."?

like image 605
Bob Avatar asked Dec 10 '08 19:12

Bob


2 Answers

the .NET string is not just an array of characters. It contains an array of characters, so strictly speaking, it's has-a.

Moreover, there are a lot of Unicode-related subtleties where it doesn't behave anything like an array. Concatenating a character may do a lot more than just increase the string length by one, and insert the new character at the end. According to the Unicode normalization rules, it may actually change the entire string. So it is definitely nothing like an array of characters, but somewhere within the class, such an array exists.

like image 132
jalf Avatar answered Nov 15 '22 05:11

jalf


It depends on your definition of the word "string".

System.String type in .NET has a character array as internal store (it also stores length (which is O(1)), among other things, for example).

But the word string means a consecutive occurrence of something in general, which might also mean a character array :))

By the way, when I said string type has a "character array," I didn't mean "a field of type char[]" specifically. I meant the general meaning of the term "array" as an ordered collection of something. :))

like image 39
mmx Avatar answered Nov 15 '22 07:11

mmx