Skip to content
David Mitchell edited this page Mar 14, 2019 · 5 revisions

Optional StringComparison arguments must always be specified

Many methods that operate on strings default to using the current culture, which is not always correct. If a method has an overload that takes a StringComparison parameter, that overload should be invoked with an explicit StringComparison specified.

if (text.StartsWith("http:")) // BAD
if (text.StartsWith("http:", StringComparison.Ordinal)) // GOOD

It is also acceptable to call an overloads that take a CultureInfo parameter.

Suggested Fixes

Add a StringComparison.Ordinal argument.

Limitations

The suggested fix always uses Ordinal comparison; in some cases, culture-sensitive comparison would be better. The fix simply appends the StringComparsion argument to the current argument list, instead of finding the "best" overload to call and mapping the current arguments to it.

Further Reading

See Also

  • FL0006 Optional IComparer<string> arguments must always be specified
Clone this wiki locally