C# + ReSharper = Awesome: Tip #4 – Convert Abstract Class to Interface

This is the fourth in a series of quick how-to articles on ReSharper.

Tip #4 – Convert Abstract Class to Interface

Use: This is used when the class(es) that will be inheriting from a base class also need to inherit from another base class. Derived types can inherit from only one base class but can implement multiple interfaces.

Before
   1:      public abstract class Book
   2:      {
   3:          public abstract string Title { get; set; }
   4:   
   5:          public abstract string Year { get; set; }
   6:   
   7:          public abstract string Author { get; set; }
   8:   
   9:          public abstract void Lend();
  10:   
  11:          public abstract void AddToInventory();
  12:      }
Right-click the class

image

After
   1:      public interface Book
   2:      {
   3:          string Title { get; set; }
   4:          string Year { get; set; }
   5:          string Author { get; set; }
   6:          void Lend();
   7:          void AddToInventory();
   8:      }

Note: Notice that this refactoring does not change the name of the type. At this point, there will be a squiggly line under the interface’s name, Book. Placing the cursor on Book and pressing <Alt+Enter> will prompt ReSharper to rename it to IBook.

 

Happy coding!

 

C# + ReSharper = Awesome: Tip #3 – Convert Into LINQ Expression

This is the third in a series of quick how-to articles on Resharper.

Tip #3 – Convert Into LINQ Expression

Use: ForEach blocks that perform simple bits of logic can often times be rewritten as lambda expressions. This reduces the number of lines of code and usually makes the code more readable.

Before
         public IList<Album> FindAlbumsToGiveAway(IList<Album> albums)
         {
             var badAlbums = new List<Album>();

             foreach (Album album in albums)
             {
                 if (album.Genre == "Country")
                     badAlbums.Add(album);
             }

             return badAlbums;
         }
Press <Alt+Enter>

image

After
         public IList<Album> FindAlbumsToGiveAway(IList<Album> albums)
         {
             return albums.Where(album => album.Genre == "Country").ToList();
         }

Happy coding!

 

del.icio.us Tags: ,,,

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: ,,