在.NET中,String和Char类型如何存储在内存中? [英] How are String and Char types stored in memory in .NET?

查看:147
本文介绍了在.NET中,String和Char类型如何存储在内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要存储一个语言代码字符串,例如en,它总是包含2个字符。



定义类型为字符串或字符?

  private string languageCode; 

vs

  private char [] languageCode; 

或者还有其他的更好的选择吗?

<这些2如何存储在内存中?当赋值时,会为它们分配多少个字节或位?

解决方案

>


$ b

字符串 char [] 存储在堆上 - 所以存储是一样的。在内部,我会假定一个字符串只是 char [] 很多额外的代码,使它对你有用。



另外,如果你有很多重复的字符串,你可以使用实习以减少这些字符串的内存占用。



更好的选择



我喜欢字符串 - 数据类型是什么以及您打算如何使用它。人们也更习惯于使用字符串,因此可维护性不会受到影响。您将从所有为您完成的样板代码中受益匪浅。微软还投入了大量精力确保字符串类型不是性能高峰。



分配大小



我不知道分配了多少,我相信字符串非常有效,因为它们只分配足够的空间来存储Unicode字符 - 因为它们是不可变的,所以这样做是安全的。数组也不能在不分配空间的情况下调整大小,所以我再次假设他们只抓取他们需要的东西。

.NET数组的另一面?



替代品根据你的信息,只有20个语言代码,性能是关键,你可以声明你自己的枚举,以减少代表所需的大小代码:

  enum LanguageCode:byte 
{
en = 0,
}

这两个 char (在一个数组中),但它确实将可用 LanguageCode 值的范围限制在 byte sizeof()来查看值类型的大小。 c $ c> operator: sizeof(LanguageCode)。枚举不过是底层的底层类型,它们默认为 int ,但正如您在我的代码示例中所看到的,您可以通过继承新类型来更改它。

I'd need to store a language code string, such as "en", which will always contains 2 characters.

Is it better to define the type as "String" or "Char"?

private string languageCode;

vs

private char[] languageCode;

Or is there another, better option?

How are these 2 stored in memory? how many bytes or bits for will be allocated to them when values assigned?

解决方案

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.

Also if you have lots of repeating strings, you can make use of Interning to reduce the memory footprint of those strings.

The Better Option

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.

The Allocation Size

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.

Overhead of a .NET array?

Alternatives

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,
}

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.

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.

这篇关于在.NET中,String和Char类型如何存储在内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆