4

I know there is no different between string and String in C# (except for the using System that is added for System.String). Still, same like in this SO-answer, I'd prefer to use string with a lowercase s as type, and String with an uppercase S when I call a String.SomeMethod (for example String.IsNullOrWhitespace(...); String.Format(...) or String.Empty.

I know I can add custom patterns in ReShaper using Visual Studio RESHARPER menu -> Option -> Code Inspection -> Custom Patterns -> Add pattern. I've added the following custom pattern:

  • Search pattern: string.$method$
  • Pattern severity: Show as suggestion
  • Match similar constructs: Unchecked
  • Search Description: Check if string.SomeMethod is used instead of String.SomeMethod
  • Replace pattern: String.$method$
  • Format after replace: Checked
  • Shorten reference: Unchecked
  • Replace Description: Use String instead of string

On a line like string.IsNullOrWhitespace(...) it does indeed give the suggestion and replaces it correctly to String.IsNullOrWhitespace(...). The problem however, is that it now also gives the suggestion when it's already correct or after I've replaced it. So is it possible to make the Search pattern case-sensitive? So it will only give the suggestion at string.SomeMethod, instead of also at String.SomeMethod?

EDIT: Also, is it possible to have an option to apply it to the entire project/solution, similar to the warning Remove unused directives in file -> Remove unused directives in solution (in v8.0 or higher)?

Community
  • 1
  • 1
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • 7
    "There isn't a lot of differences between string and String in C#." - There is **NO difference**. `string` is an alias for `String`. – CriketerOnSO Dec 10 '14 at 15:37
  • 3
    @CriketerOnSO Yet you can have a preference of using one or the other and if you do, it would be wise to be consistent throughout your code base; so it's not an unreasonable question. – decPL Dec 10 '14 at 15:45
  • 1
    @decPL, I am not saying that it is a unreasonable question, I was just referring to the statement in question, *(which has now been edited out)* – CriketerOnSO Dec 10 '14 at 17:30

1 Answers1

1

The reason why you are getting this behavior is because string and String are identical and ReSharper quite often performs type reductions behind the scenes. SSR is one such place where lots of generalizations and possible rearrangements take place. To cut the long story short, SSR is designed for syntactic changes, and what you are (effectively) doing is performing a purely symbolic change that doesn't actually change the code as far as anyone is concerned.

Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166