I just want to be sure:
string X="";
char Char=X[0]; //an exception "Index was outside the bounds of the array"
This means that the string is really treated as an array of chars, right? At least internally.
I just want to be sure:
string X="";
char Char=X[0]; //an exception "Index was outside the bounds of the array"
This means that the string is really treated as an array of chars, right? At least internally.
The C# language spec makes no guarantee about the internal representation of a string. However, it implements the index operator to provide a char for each character in the string.
Edit: To clarify since a few people have commented, yes, the internal representation of System.String in the CLR is an array. However, the language specification doesn't say anything about internal representation, so this could (but is unlikely to) change. It says that a string has to work as a sequence of chars. The only bit about this in the language spec is under section 1.3:
Character and string processing in C# uses Unicode encoding. The char type represents a UTF-16 code unit, and the string type represents a sequence of UTF-16 code units.
Additionally, MSDN states:
A string is a sequential collection of Unicode characters that is used to represent text. A String object is a sequential collection of System.Char objects that represent a string. The value of the String object is the content of the sequential collection, and that value is immutable (that is, it is read-only).
So in this case, we're now talking about the CLR and not the language. System.String
-- However, even there they don't guarantee an array, only a sequential collection.
A string implemented with a linked list and an indexer that moved n
spaces forward in the list would be sufficient to satisfy the language reqiurements. IList<char>
would also satisfy the requirements, and IList
doesn't have to be array-backed.
You might find this MSDN doc helpful.
In a nutshell, a string is "stored as a sequential read-only collection of Char objects"
And, yes, it can be accessed just like a char array. So, if X contained a value other than String.Empty, then the char Char=X[0;
] code would have returned the first character of the string.
As far as I know, that's correct. Btw here's a page with everything you ever wanted to know about Strings:
Per @JaredPar elsewhere on this site:
The underyling string you create will also need a contiguous block of memory because it is represented as an array of chars (arrays require contiguous memory) .
I am sure you should not rely on this as it's not part of the interface, but implementation is an array if this statement is correct. That makes sense to me given what we know about char-strings and Microsoft's need to support efficient interop between managed and native languages.
MSDN says only this, which does not guarantee that the storage is an array.
A string is a sequential collection of Unicode characters that is used to represent text. A String object is a sequential collection of System.Char objects that represent a string. The value of the String object is the content of the sequential collection, and that value is immutable (that is, it is read-only).
C# is just the language. The string keyword is an alias for System.String in the BCL of .Net framework. It is pretty safe to assume that internally String is an array of chars. From MSDN:
A string is a sequential collection of Unicode characters that is used to represent text. A String object is a sequential collection of System.Char objects that represent a string.
It depends on what you mean by "array".
If you mean the general computing concept of a random-access, fixed-length, integer-indexable collection of objects, then yes, a string can be considered precisely like that. (The general computing concept often includes being contiguous in memory, but barring a few cases, such as using pointers in unsafe code, that's not very meaningful in terms of C#).
If you mean the language-defined C# implementation of this concept, char[]
then not really, the two are different things.
In practice, System.String
is indeed implemented as an array of char
s, but it doesn't have to have been.
Language nit-picks aside, the practical bit:
If you want to do the same operations on a string as you would on a char[]
then this will often work (notably though, string is read-only) and very often be the most efficient way of doing so, as long as conceptually quite simple. In particular, using foreach
and using an index that moves between 0
and str.Length - 1
work well. Similarly, a lot of operations one can do on char[]
can be done on string
, such as CopyTo()
and casting to IEnumerable<char>
.
If you want to actually have an array of chars then you need to call ToCharArray()
.