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

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

    System.String is not a .NET array of Char because this:

    char[] testArray = "test".ToCharArray();
    
    testArray[0] = 'T';
    

    will compile, but this:

    string testString = "test";
    
    testString[0] = 'T';
    

    will not. Char arrays are mutable, Strings are not. Also, string is Array returns false, while char[] is Array returns true.

提交回复
热议问题