The string
type offers an indexer property that allows you to treat it as a char
array. Therefore, if you just need to
access a specific character or iterate over all of them, the ToCharArray
call should be omitted. For these cases, not omitting makes the
code harder to read and less efficient as ToCharArray
copies the characters from the string
object into a new Unicode
character array.
The same principle applies to utf-8 literals
types (ReadOnlySpan<byte>
, Span<byte>
) and the ToArray
method.