Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suspicious warning from Resharper - should I change my code?

With the following code:

if (lombardiTrophy.Substring(1, 1).Equals('~'))

...I get, "Suspicious comparison: there is no type in the solution which is inherited from both 'string' and 'char'"

Is there a better way to do this that wouldn't cause Resharper to raise its hackles?

like image 342
B. Clay Shannon-B. Crow Raven Avatar asked Sep 06 '12 22:09

B. Clay Shannon-B. Crow Raven


2 Answers

You should heed ReSharper's warning - Substring returns a string, and the single quote notation is a char, so you're comparing two different types. You should compare a char to a char, which you can do like this:

if (lombardiTrophy[1].Equals('~'))

Of course you want to make sure that your string is at least two characters long.

like image 138
Evan M Avatar answered Sep 28 '22 09:09

Evan M


Try this:

 if (lombardiTrophy.Substring(1, 1).Contains("~"))

Note the double quotes for string comparison.

like image 22
Peter Gluck Avatar answered Sep 28 '22 07:09

Peter Gluck