I found a use today for a new feature that will be in ReSharper 5.0 - “Structural Search and Replace”. Essentially it’s a way to add code matching templates into R# to add new refactoring patterns in addition to those that come in the box.

For example, Microsoft recommends (and Code Analysis/FxCop generates appropriate warnings) if you are doing a comparison between a string value and an empty string, to use the String.IsNullOrEmpty() method.

I always wanted R# to make this change for me, and now I can:

  1. Go to Resharper Tools Patterns Catalog
  2. Click on ‘Add Pattern’
  3. Enter “$value$ == "" “ in the Search Pattern textbox.
  4. Click on ‘Add Placeholder’ and add an ‘Expression’ placeholder named ‘value’ of type System.String
  5. Enter “string.IsNullOrEmpty($value$)” in the Replace Pattern textbox
  6. Ensure Pattern Severity is set to at least ‘Show as suggestion’
  7. Click on ‘Add’

R# will now use this pattern and highlight code that matches. You can then hit the QuickFix shortcut (Alt-Enter by default) and get a smart tag offering to refactor/replace your code.

 Resharper 5 - Smart Tag showing option to replace with String.IsNullOrEmpty

If you can’t wait for the final release of ReSharper 5 (due in the next few months) then install beta 2 or one of the recent nightly builds.