Refactoring With ReSharper, Some Very Nice C# fixes I often use
Thursday 3 July 2008 @ 6:59 am

One of the really nice features I enjoy using in Resharper 4.0 is the refactoring that converts inefficient (and unpleasant to read) syntax into nice efficient code.  In this post, I’ll show some refactorings that Resharper (from JetBrains) gives us.  There are lots more, but the ones listed below seem to come up the most in my own c# code.

 

Creates String.Format in C# From “a” + “b” + “c” syntax

From:

var url = wikiURL + "/api/AddPage?apikey_v1=" + apiKey
                                + "&page=" + wikiPageName
                                + "&name=" + name
                                + "&email=" + email; 

To:

var url =  string.Format("{0}/api/AddPage?apikey_v1={1}&page={2}&name={3}&email={4}",
                    wikiURL, apiKey, wikiPageName, name, email); 

 

Create Simple Logical Assignments From my Overly Verbose Code

From:

if (ConfigurationManager.AppSettings["ShowDinnerConfirmation"].ToLower().Equals("true"))
            {
                TableRowSaturdayDinner.Visible = true;
            }
            else
            {
                TableRowSaturdayDinner.Visible = false;
            } 

 

To:

TableRowSaturdayDinner.Visible = ConfigurationManager.AppSettings["ShowDinnerConfirmation"].ToLower().Equals("true"); 

 

Use ?? When I’ve used Simple ? Operator Makes for Simpler Code

From:

string s = context.Request.QueryString["Cache"] == null ? "true" : context.Request.QueryString["Cache"];

To:

string s = context.Request.QueryString["Cache"] ?? "true";
- Posted in Best Practices, C#, Tools  




2 Responses to “Refactoring With ReSharper, Some Very Nice C# fixes I often use”

  1. Mohammad Azam Says:

    Hi Peter,

    I installed Resharper but I had to uninstall it because it was making my VS too slow. The intellisense was completely dead and took a long time to open. Do you have any ideas about these issues?

  2. Barry Says:

    I got this ‘cos I also had Visual Assist installed - I guess both Refactor and VA were trying to do the same thing.

    HTH?

    Barry

Leave a Reply