Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point using String.ToCharArray if a string is a char array itself?

Tags:

c#

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?

like image 339
iTayb Avatar asked Mar 19 '10 12:03

iTayb


2 Answers

A string is not a char array. You are confusing the fact that it has an indexer returning char with it being a char array.

like image 146
John Saunders Avatar answered Oct 20 '22 19:10

John Saunders


Just because you can write s[1] doesn't mean that a string is a char array, it meerely means string has an indexer that returns a char. The fact that indexers are accessed with the same syntax as array member access is a C# language feature.

like image 21
AakashM Avatar answered Oct 20 '22 18:10

AakashM