C# + ReSharper = Awesome: Tip #9 – Move Types into Matching Files

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

Tip #9 – Move Types into Matching Files

Use: Let’s say you’re coding away on a class, and you’re doing things like letting Visual Studio or ReSharper create additional types in the same code file based on usage. Suddenly, you have three or four classes in the same code file. Now it’s time to step back and get things organized. From JetBrains:

This refactoring can applied to a single file or a selection of files that have multiple types each. ReSharper creates dedicated files for each of these types are moves them there.

Before
   1:      public class Movie : IMovie
   2:      {
   3:          private IList<IActor> actors;
   4:   
   5:          public Movie(IList<IActor> actors)
   6:          {
   7:              this.actors = actors;
   8:          }
   9:   
  10:          public ProductionStatus Status { get; set; }
  11:   
  12:          public IList<IActor> Actors
  13:          {
  14:              get { return this.actors; }
  15:          }
  16:      }
  17:   
  18:      public enum ProductionStatus
  19:      {
  20:          NotYetInProduction,
  21:          PreProduction,
  22:          Production,
  23:          PostProduction,
  24:          Released
  25:      }
  26:   
  27:      public interface IMovie
  28:      {
  29:          ProductionStatus Status { get; set; }
  30:          IList<IActor> Actors { get; } 
  31:      }
  32:   
  33:      public interface IActor
  34:      {
  35:          string Name { get; }
  36:      }
Right-click the code file in Solution Explorer

image

Select options and finish

SNAGHTML34215b3b

After

image

New contents of Movie.cs
   1:      public class Movie : IMovie
   2:      {
   3:          private IList<IActor> actors;
   4:   
   5:          public Movie(IList<IActor> actors)
   6:          {
   7:              this.actors = actors;
   8:          }
   9:   
  10:          public ProductionStatus Status { get; set; }
  11:   
  12:          public IList<IActor> Actors
  13:          {
  14:              get { return this.actors; }
  15:          }
  16:      }

Happy coding!