Skip to content

Refactoring With ReSharper, Some Very Nice C# fixes I often use

Updated: at 02:12 PM

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";

Check out the ORM (Object Relational Mapper) PRISMA. The database access method I use in all my projects