Pattern: Missing use of String.StartsWith()
Issue: -
It's more efficient and clearer to call String.StartsWith
than to call String.IndexOf
and compare the result with zero to determine whether a string starts with a given prefix.
IndexOf
searches the entire string, while StartsWith
only compares at the beginning of the string.
Example of incorrect code:
bool M(string s)
{
return s.IndexOf("abc") == 0;
}
Example of correct code:
bool M(string s)
{
return s.StartsWith("abc");
}