Skip to content
David Mitchell edited this page Dec 28, 2018 · 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

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.

Clone this wiki locally