问题描述
我需要存储一个语言代码字符串,例如en",它总是包含 2 个字符.
I'd need to store a language code string, such as "en", which will always contains 2 characters.
将类型定义为String"还是Char"更好?
Is it better to define the type as "String" or "Char"?
private string languageCode;
对
private char[] languageCode;
或者还有其他更好的选择吗?
Or is there another, better option?
这 2 个如何存储在内存中?分配值时将分配多少字节或位?
How are these 2 stored in memory? how many bytes or bits for will be allocated to them when values assigned?
推荐答案
它们是如何存储的
string
和 char[]
都存储在堆上 - 所以存储是相同的.在内部,我会假设 string
只是 char[]
的封面,带有 lots 的额外代码以使其对您有用.
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.
如果你有很多重复的字符串,你可以使用 实习以减少这些字符串的内存占用.
Also if you have lots of repeating strings, you can make use of Interning to reduce the memory footprint of those strings.
更好的选择
我更喜欢字符串 - 数据类型是什么以及您打算如何使用它会更加明显.人们也更习惯于使用字符串,因此可维护性不会受到影响.您还将从为您完成的所有样板代码中受益匪浅.Microsoft 也付出了很多努力来确保 string
类型不会影响性能.
I would favour string - it is immediately more apparent what the data type is and how you intend to use it. People are also more accustomed to using strings so maintainability won't suffer. You will also benefit greatly from all the boilerplate code that has been done for you. Microsoft have also put a lot of effort in to make sure the string
type is not a performance hog.
分配大小
我不知道分配了多少,我相信字符串非常有效,因为它们只分配足够的空间来存储 Unicode 字符——因为它们是不可变的,所以这样做是安全的.如果不分配新数组中的空间,数组也无法调整大小,所以我再次假设它们只抓取所需的内容.
I have no idea how much is allocated, I believe strings are quite efficient in that they only allocate enough to store the Unicode characters - as they are immutable it is safe to do this. Arrays also cannot be resized without allocating the space in a new array, so I'd again assume they grab only what they need.
.NET 数组的开销?
替代品
根据您的信息,只有 20 种语言代码并且性能是关键,您可以声明自己的枚举以减少表示代码所需的大小:
Based on your information that there are only 20 language codes and performance is key, you could declare your own enum in order to reduce the size required to represent the codes:
enum LanguageCode : byte
{
en = 0,
}
这将只占用 1 个字节,而两个 char
(在一个数组中)则需要 4+ 个字节,但它确实将可用 LanguageCode
值的范围限制在该范围内byte
- 足以容纳 20 个项目.
This will only take 1 byte as opposed to 4+ for two char
(in an array), but it does limit the range of available LanguageCode
values to the range of byte
- which is more than big enough for 20 items.
您可以使用 sizeof()
运算符查看值类型的大小:sizeof(LanguageCode)
.枚举只不过是底层的类型,它们默认为 int
,但正如您在我的代码示例中看到的,您可以通过继承"新类型来更改它.
You can see the size of value types using the sizeof()
operator: sizeof(LanguageCode)
. Enums are nothing but the underlying type under the hood, they default to int
, but as you can see in my code sample you can change that by "inheriting" a new type.
这篇关于String 和 Char 类型如何存储在 .NET 的内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!