C#

C#

Made by DeepSource

Redundant call to ToCharArray() when iterating a string CS-P1004

Performance
Major
Autofix

The ToCharArray() returns a char array whose elements are the individual characters of the string on which this method is called. However, this call is particularly redundant within a foreach statement as foreach allows you to iterate through the types that implement IEnumerable or IEnumerable<T>, such as string in this case. Therefore, it is recommended that you get rid of this redundant call.

Bad Practice

foreach (char ch in someString.ToCharArray())
{
    // Some logic here
}

Recommended

foreach (char ch in someString)
{
    // Some logic here
}