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

后端 未结 12 1843
無奈伤痛
無奈伤痛 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:27

    Everyone has given half the answer, so here is both parts:

    1) Strictly speaking, yes, a String in .NET is an array of characters. It is so both in its internal implementation, and by the symantic definition of an array.

    2) However String is, as others have pointed out, somewhat peculiar. It is not a System.Array as all other arrays are. So in the strict, .NET specific way, a String is not an Array.

提交回复
热议问题