All rules
CA1858Performance Enabled by default: As suggestion

Use StartsWith instead of IndexOf

Use StartsWith instead of IndexOf

Microsoft docs

Description

It's more efficient and clearer to call System.String.StartsWith than to call System.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.

Cause

System.String.IndexOf is called and its result is compared against zero.

How to fix violations

Replace the call to System.String.IndexOf with a call to System.String.StartsWith.

Example

bool M(string s)
{
    return s.IndexOf("abc") == 0;
}

bool M(string s)
{
    return s.StartsWith("abc");
}

When to suppress

It's safe to suppress this warning if performance isn't a concern.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0