Is string actually an array of chars or does it just have an indexer?

后端 未结 12 1817
無奈伤痛
無奈伤痛 2020-12-06 09:58

As the following code is possible in C#, I am intersted whether string is actually an array of chars:

string a=\"TEST\";
char C=a[0]; // will be T

12条回答
  •  無奈伤痛
    2020-12-06 10:22

    A string is not a char[], although it does have a .ToCharArray(). Also it does have an indexer, which allows you to access characters individually, like you've shown. It is likely that it was implemented with an array internally, but that's an implementation detail.

提交回复
热议问题