What are strings in .NET?

Is the string actually a character array (is-a) or has an array of characters as internal storage (has-a), or is it a native object that can expose itself as a with an array of characters?

I am more inclined to say that this is our own object, but then why are we so inclined to always say: "A string is an array of characters ..."?

+5
source share
9 answers

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

, , Unicode, . , . Unicode . , , - .

+7

"".

System.String .NET ( (, O (1)), ).

- , :))

, , string " ", " char[]". "" -.:))

+7

- .

.Net String (has-a) , .

: " Apple ? , ".

+3

MSDN: .

: Unicode.

+1

(, , ) - .

, , .

0

String - . . unicode ASCII , , . , , . .

0

. (ascii char * string) , int, ascii, (char (0)). , , , char * .

0

, . GCHandle, , , , 32- , Unicode ( , AddrOppinedObject , . , P/Invoking).

0

. (, C), . , .

However, for other purposes, saving Unicode strings as UTF-8 may be the most appropriate form. Note that although it is stored in a byte array, there is no longer a one-to-one correspondence between bytes and characters: your string algorithms should usually access characters sequentially from the very beginning - in the form of a list.

The moral of this story is: your string code should only require random access, if you really need it. You may be surprised how rarely you really need an array of characters.

0
source

All Articles