C#

C#

By DeepSource

Consider using the appropriate overloaded method when searching for a single charCS-P1006

Performance
Has Autofix

Methods such as string.Contains and string.IndexOf allow you to search for an occurrence of either a single char or a substring within a string. If you'd like to search for an occurrence of a single char, it is recommended that you use the appropriate method that takes a char as a parameter rather than a string as the former approach is more performant and recommended.

Example

Bad practice

var lang = "csharp";

// Searching for `a` using the method that takes a string as a parameter.
// Although this works, it is not an efficient approach.
var idx  = lang.IndexOf("a");

Recommended

var lang = "csharp";

// Searching for `a` using the method that takes a Char as a parameter.
// This is the recommended performant approach.
var idx  = lang.IndexOf('a');

Reference