evanmcdonnal
evanmcdonnal

Reputation: 48086

Accessing a character in a string vs. converting to character array in C#

In answering another question someone pointed out that in C# you can access a character in string by doing sting[i]. My question is, what is happening under the covers? Is this any different than converting the string to a character array and then parsing it?

I assume the difference is in memory usage and mutability but I'd rather know than assume :)

Upvotes: 3

Views: 224

Answers (1)

to StackOverflow
to StackOverflow

Reputation: 124696

The main difference is that converting to a character array will create a copy of the string's internal character array, whereas using the indexer will access the characters in place.

Upvotes: 6

Related Questions