C# + ReSharper = Awesome: Tip #1 – To Automatic Property


This is the first in a series of quick how-to posts on ReSharper. I love ReSharper. It is a tool that I use every day and don’t really realize how much I rely on it until I use a machine without ReSharper installed.

Tip #1 – To Automatic Property

Use: If a public property does not contain any logic, it can be converted to an auto-property, removing the corresponding private field and replacing usages of the private field.

Before
   1:          private string _name;
   2:          public string Name
   3:          {
   4:              get { return _name; }
   5:              set { _name = value; }
   6:          }
   7:   
   8:          public void CheckName()
   9:          {
  10:              if (_name == "Dark Side of the Moon")
  11:                  Console.WriteLine("Awesome");
  12:          }

Place your cursor on the Name property and…

Press <Alt-Enter>

image

After
   1:          public string Name { get; set; }
   2:   
   3:          public void CheckName()
   4:          {
   5:              if (Name == "Dark Side of the Moon")
   6:                  Console.WriteLine("Awesome");
   7:          }
 

Happy coding!

 

del.icio.us Tags: ,

1 comment so far

  1. Rory Primrose December 12, 2011 11:19 pm

    What’s even more awesome is that this feature participates in the code cleanup profile options. Ctrl + E, F is my friend :)

Leave a comment

Please be polite and on topic. Your e-mail will never be published.

 

About Me

I am a Philadelphia-area .NET developer, Microsoft MVP, husband, dad, and geek. I am currently a software engineer at Eclipsys Corporation. I have over 15 years of software development experience in the Healthcare and Manufacturing industries.

More...

Categories

Archives