How are String and Char types stored in memory in .NET?

后端 未结 6 1294
自闭症患者
自闭症患者 2020-12-03 18:05

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\"?

<
6条回答
  •  [愿得一人]
    2020-12-03 18:40

    Strings indeed have a size overhead of one pointer length, i.e. 4 bytes for a 32 bit process, 8 bytes for a 64 bit process. But then again, strings offer so much more in return than char arrays.

    If your application uses many short strings and you don't need to use their string properties and methods that often, you could probably safe a few bytes of memory. But if you want to use any of them as a string, you will first have to create a new string instance. I can't see how this will help you safe enough memory to be worth the trouble.

提交回复
热议问题