C#中:什么会占用更多的内存?字符串或ByteArray?字符串、内存、更多、ByteArray

2023-09-04 09:33:24 作者:糖不及你甜

C#:是什么占用更多的内存?一个字符串或字节?

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?

推荐答案

字节数组。这将存储您的文本为ASCII(每个字符1字节)字符,而.NET字符串使用统一code这是更大的。但是请记住,.NET字符串可能更有用,并在大型应用程序的差别可能不会产生巨大的变化。

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.

(请注意,如果你只是用ASCII字符在.NET字符串,然后字符将仍然只有1个字节)

(note also that if you just use ASCII characters in your .NET string then the characters will still only be 1 byte each)