Suspicious warning from Resharper - should I change my code?

With the following code:

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

... I get: "Suspicious comparison: the solution does not have a type inherited from" string "and" char "

Is there a better way to do this that won't force Resharper to pick up its hacks?

+6
source share
2 answers

You should heed the ReSharper warning. The substring returns a string, and the single quote notation is char, so you are comparing two different types. You should compare char with char, which you can do as follows:

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

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

+8
source

Try the following:

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

Pay attention to double quotes for string comparison.

+6
source

Source: https://habr.com/ru/post/924806/


All Articles