What is the point of using String.ToCharArray if the string is a char array?

string s = "string"; Console.WriteLine(s[1]); // returns t char[] chars = s.ToCharArray(); Console.WriteLine(chars[1]); // also returns t 

So what is the point in this method?

+6
c #
source share
3 answers

The string is not a char array. You are confused by the fact that it has a pointer that returns char, since it is a char array.

+11
source share

Just because you can write s[1] does not mean that the string a char, this means that string has an indexer that returns a char . The fact that indexers are available with the same syntax as accessing an array member is a C # language function.

+5
source share

System.String is not an array of characters. If you need an array of characters (for example, to pass as an argument to the method that expects it), you will need to do this conversion.

+3
source share

All Articles