C#: What takes up more memory? A string or bytes?
Let's say I have a line that reads "My Text", in which form would that line use up more memory, as a byte or a string?
C#: What takes up more memory? A string or bytes?
Let's say I have a line that reads "My Text", in which form would that line use up more memory, as a byte or a string?
Both are pretty close. Only one real answer:
Profile it on your framework/architecture.
The byte array would take less memory unless you had several copies of the string, in which case the string would take up less memory thanks to the string table.
But the real questions is, does it really matter? There are a lot of benefits you get to using the string as a string, rather than storing it as an array of bytes.
I don't know the particulars, since your question was very narrow, but I smell premature optimization.
The byte array. This will store your text as ASCII (1 byte per character) characters, whereas a .NET string uses Unicode which are larger. However remember that .NET strings are probably more useful and in a large application the difference probably won't make a huge difference.
(note also that if you just use ASCII characters in your .NET string then the characters will still only be 1 byte each)
It depends on the character encoding of the byte array. You can convert any string into an array of bytes, but you have to choose the encoding; there is no single standard or correct encoding. What used to be called ASCII is no use outside of the English speaking world.
In most encodings, "My Text" would be 7 bytes long. But throw in some European accented characters, or Japanese characters, and those (if they can be represented at all) may be more than one or two bytes each. In some encodings, with some text strings, the byte-array representation may be larger than the internal Unicode representation used by System.String
.
Being Unicode doesn't mean that the string will take more than one byte per character, it just means it "could" take up more than one byte per character.