C# + ReSharper = Awesome: Tip #2 – Create Field

This is the second in a series of quick how-to posts on ReSharper.

Tip #2 – Create Field

Use: Within the body of a class or property, you can type the name of a non-existent variable name. When you place your cursor on the variable, ReSharper provides several options to resolve the impending compilation error. Create Field is one of the options.

Before
   1:          public void AddAlbum(string album)
   2:          {
   3:              if (String.IsNullOrWhiteSpace(album))
   4:                  throw new ArgumentOutOfRangeException("album",
   5:                                                        album,
   6:                                                        "Please provide an album name.");
   7:   
   8:              _albums.Add(album);
   9:          }
Press <Alt+Enter>

image

After
        private IList<string> _albums = new List<string>();

        public void AddAlbum(string album)
        {
            if (String.IsNullOrWhiteSpace(album))
                throw new ArgumentOutOfRangeException("album",
                                                      album,
                                                      "Please provide an album name.");

            _albums.Add(album);
        }

Note: I chose the type IList<string>. The default type selected by ReSharper in this case was object. I also added the initializer to the field to avoid null reference exceptions. Please don’t take any of these examples as best coding practices, they are contrived to demonstrate a particular refactoring.

Happy coding!

 

del.icio.us Tags: ,,

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
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public void CheckName()
        {
            if (_name == "Dark Side of the Moon")
                Console.WriteLine("Awesome");
        }

Place your cursor on the Name property and…

Press <Alt-Enter>

image

After
        public string Name { get; set; }

        public void CheckName()
        {
            if (Name == "Dark Side of the Moon")
                Console.WriteLine("Awesome");
        }

Happy coding!

 

del.icio.us Tags: ,

Dew Drop – December 7, 2011

Top Links

 

.NET / Visual Studio

 

Web Development

 

Design / Methodology / Testing

 

Silverlight / WPF / Windows Phone

 

Podcasts / Screencasts / Videos

 

Community / Events

 

Database

 

SharePoint

 

Miscellaneous

 

More Link Collections

 

The Geek Shelf

Silverlight 5 in Action by Pete Brown

 

Mastodon
github.com/alvinashcraft