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

后端 未结 6 1297
自闭症患者
自闭症患者 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:25

    String just implements an indexer of type char internally and we can say that string is just equivalent to char[] type with lots of extra code to make it useful for you, hence, like an array, it is stored on heap always.

    An array cannot be manipulated without allocating it new space, same will be the case of a string hence, it is immutable

    String implements IEnumerable

    Noticeable point: When you pass a string to a function, it is a pass by value unless there is a use of ref

提交回复
热议问题