According to your comment:
In one post it says the string is stored as char[] and another says it isnt stored as array. So I am confused.
No one ever said that strings are stored as char[]
. Never. Let's look at the contents of the first post:
How They Are Stored
Both the string
and the char[]
are stored on the heap - so storage is
the same. Internally I would assume a string
simply is a cover for
char[]
with lots of extra code to make it useful for you.
See the word "assume"? Because string
s are extremely similar to a char[]
, in our minds, we can just "assume" that they are the same. Does this mean that string
is the same as a char[]
? No.
Also, this sentence:
Both the string
and the char[]
are stored on the heap - so storage is
the same.
Just says that they are both stored in the same place, but doesn't say anything about how they are stored.
The two explanations hence don't conflict.
Jon Skeet has a great explanation about string and memory here.