-3

After reading 2 different post on how the string is stored I am little confused which one is right.

Please find the below links:
How are String and Char types stored in memory in .NET?
C# - is string actually an array of chars or does it just have an indexer?

Bhaktuu
  • 89
  • 1
  • 7
  • The two explanations don't conflict. What are you confused about? – Sweeper Jun 27 '17 at 06:27
  • 1
    What are you asking? – Koby Douek Jun 27 '17 at 06:27
  • In one post it says the string is stored as char[] and another says it isnt stored as array. So I am confused. – Bhaktuu Jun 27 '17 at 06:29
  • 6
    Why do you care? Strings are guaranteed to follow the contract given by the documentation for strings. Why do you need to know the implementation details? What decision will you make differently once you know the answer to your question? Could you devise a behavioural test that would give one result if strings were stored as arrays and a different result if otherwise? If yes, then you can answer your question by doing the test. If not, then why does it make a difference? A difference where there is no *observable* difference is not an interesting difference. – Eric Lippert Jun 27 '17 at 06:29
  • I am trying to understand how they are stored – Bhaktuu Jun 27 '17 at 06:30
  • In c# a char is a class object which is either one or two bytes with a private property that indicates if the char is one or two bytes. The Encoding classes have access to the private property to set to either one or two bytes. The string class is an array of char with additional methods to perform actions to the class. – jdweng Jun 27 '17 at 06:34
  • 3
    @Bhaktuu: Nothing in jdweng's comment is correct. – Eric Lippert Jun 27 '17 at 06:35
  • @EricLippert I was reading about string, So I wanted to know how string is actually stored. I dont want to make any decision out of it.I found that expanation in 2 posts is contracdictory. So I have posted this question – Bhaktuu Jun 27 '17 at 06:38
  • Then my advice is to do two things: (1) get the source code for the string class and read it carefully, and (2) get a debugger and examine the memory used to store some strings. Between those two things you will soon know exactly how strings are stored to whatever level of detail you are interested in. – Eric Lippert Jun 27 '17 at 06:49
  • @EricLippert Thank you for your help! – Bhaktuu Jun 27 '17 at 06:50

2 Answers2

4

You can have a look at dot net core source code (I suppose - maybe I'm wrong - this part is common with .NET framework). A .NET String is internally defined as an unmanaged instance of "StringObject"

https://github.com/dotnet/coreclr/blob/8cc7e35dd0a625a3b883703387291739a148e8c8/src/vm/object.h

/*
 * StringObject
 *
 * Special String implementation for performance.   
 *
 *   m_StringLength - Length of string in number of WCHARs
 *   m_Characters   - The string buffer
 */

class StringObject : public Object
{
  ...
  private:
    DWORD   m_StringLength;
    WCHAR   m_Characters[0];
  ...
}

This is all unmanaged. It contains a buffer that happens to be defined as an array of wide (Unicode) characters, but that doesn't mean it cannot be used differently (like an array of bytes, or whatever, because unmanaged is a wild world).

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
0

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 strings 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.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • In the first post there was a line "Internally I would assume a string simply is a cover for char[] with lots of extra code to make it useful for you". So I was confused. – Bhaktuu Jun 27 '17 at 06:53
  • @Bhaktuu Yeah exactly! See the word "assume"? Because they are very similar in functionality, we can assume that they are the same. Does that mean they really _are_ the same? No. – Sweeper Jun 27 '17 at 06:55
  • Yes Ofcos I read it and so I was confused. – Bhaktuu Jun 27 '17 at 06:58
  • @Bhaktuu Basically, the post is not saying that char arrays and strings are stored in the same way. That post does not really answer the question "how arrays are stored". If you want to know more, go read Jon Skeet's blog post. – Sweeper Jun 27 '17 at 07:00