3

I had this code:

var whackPos = str.IndexOf("/");

...which Resharper recommended be changed to:

var whackPos = str.IndexOf("/", StringComparison.Ordinal);

I see why it is good sometimes, and the different between it and InvariantCulture here, but I don't see why this would be recommended for a little old whack ("/").

Is this really a safer way of finding forwardwhacks, or a case of Resharper being overly persnickety?

UPDATE

For some things, Resharper recommends InvariantCulture. e.g., this code:

percentageQtyShippedCell.Value2 = _percentageOfQtyShipped.ToString();

...was changed to this:

percentageQtyShippedCell.Value2 = _percentageOfQtyShipped.ToString(CultureInfo.InvariantCulture);

...at Resharper's perspicacious proddings. So why does it sometimes opt for InvariantCulture, and other times for Ordinal?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

5

The one-parameter version of IndexOf() uses culture-specific comparison. It will behave differently depending on where you code runs. In many cases Ordinal or InvariantCulture comparison is more appropriate. This is what Resharper recommends. When you look for '/', Ordinal is sufficient and it also happens to be the simplest and fastest comparison type, so that's what Resharper recommends.

For cases where you do want to use culture-sensitive comparisons, just specify StringComparison.CurrentCulture, and Resharper will assume that you know what you are doing. I know you did not ask about this last part. Just adding it for completeness.

George
  • 2,436
  • 4
  • 15
  • 30