Skip to content

Files

Latest commit

 

History

History
31 lines (21 loc) · 672 Bytes

File metadata and controls

31 lines (21 loc) · 672 Bytes

Pattern: Missing use of String.StartsWith()

Issue: -

Description

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");
}

Further Reading