Sample case-sensitive string comparison pattern

I am trying to add a Resharper template to the template directory to compare strings. In the end, I would like him to warn me when I jump into a code like string1 == string2 and suggest replacing it with string1.Equals(string2, StringComparison.OrdinalIgnoreCase) . From here I can change it to the appropriate case, but the initial step of enforcing the use of Equals with comparison options makes the intention of comparison obvious.

I can do this by creating a search pattern:

 $string1$ == $string2$ 

And creating a Replace template:

 $string1$.Equals($string2$, StringComparison.OrdinalIgnoreCase) 

Where both $ string1 $ and $ string2 $ are expressions of the type "String (or derived type)". This is a great start, because even if I wanted it to be case sensitive, I can see the options and change them.

The problem I am facing is that now it also warns me about string == null . Is there a way to make a null exception in a search?

+4
source share
1 answer

As far as I can tell from the documentation , it is impossible to establish conditions such as "not null".

If you feel brave, you can first replace

 $string$ == null 

with

 String.IsNullOrEmpty($string$) 

where necessary. But that does not mean exactly the same.

0
source

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


All Articles