C# + ReSharper = Awesome: Tip #10 of 10 – Generate Equality Members

This is the tenth and final in a series of quick how-to articles on ReSharper. There are obviously many more awesome features to explore, but I want to give equal time to the other available productivity tools for Visual Studio. Next week I will be starting a new series of tips on Telerik JustCode. Following that series, I will take a look at DevExpress CodeRush.

Tip #10 – Generate Equality Members

Use: If you are creating a class that will need to participate in equality operations, ReSharper can quickly generate the necessary method implementations for you.

Before
   1:      public class Order : IOrder
   2:      {
   3:          public int OrderId { get; set; }
   4:   
   5:          public int CustomerId { get; set; }
   6:   
   7:          public DateTime OrderDate { get; set; }
   8:   
   9:          public IList<OrderDetail> OrderDetails { get; set; }
  10:      }
Click Generate Code

image

Select Equality Members

image

Select options and finish

SNAGHTML337d04cf

After
   1:      public class Order : IOrder, IEquatable<Order>
   2:      {
   3:          public int OrderId { get; set; }
   4:   
   5:          public int CustomerId { get; set; }
   6:   
   7:          public DateTime OrderDate { get; set; }
   8:   
   9:          public IList<OrderDetail> OrderDetails { get; set; }
  10:   
  11:          public bool Equals(Order other)
  12:          {
  13:              if (ReferenceEquals(null, other)) return false;
  14:              if (ReferenceEquals(this, other)) return true;
  15:              return other.OrderId == OrderId;
  16:          }
  17:   
  18:          public override bool Equals(object obj)
  19:          {
  20:              if (ReferenceEquals(null, obj)) return false;
  21:              if (ReferenceEquals(this, obj)) return true;
  22:              if (obj.GetType() != typeof (Order)) return false;
  23:              return Equals((Order) obj);
  24:          }
  25:   
  26:          public override int GetHashCode()
  27:          {
  28:              return OrderId;
  29:          }
  30:   
  31:          public static bool operator ==(Order left, Order right)
  32:          {
  33:              return Equals(left, right);
  34:          }
  35:   
  36:          public static bool operator !=(Order left, Order right)
  37:          {
  38:              return !Equals(left, right);
  39:          }
  40:      }

Happy coding!

 

C# + ReSharper = Awesome: Tip #8 – Extract Class From Parameters

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

Tip #8 – Extract Class From Parameters

Use: If you find yourself passing a set of related values to a method, you should probably put all of those items into a single class or struct. From JetBrains:

This refactoring creates a new class or struct and converts parameters of the selected method into encapsulated fields of the newly created type (with constructor taking parameters, fields to store values and properties to retrieve values). Usages of parameters are converted to usages of properties of created type.

Before
   1:      public class EmployeeUtilities
   2:      {
   3:          public bool ValidateTerminatedEmployeeInfo(int employeeId
   4:                                                   , string firstName
   5:                                                   , string lastName
   6:                                                   , DateTime hireDate
   7:                                                   , DateTime terminateDate)
   8:          {
   9:              if (employeeId > -1 && 
  10:                  !String.IsNullOrWhiteSpace(firstName) && 
  11:                  !String.IsNullOrWhiteSpace(lastName) &&
  12:                  hireDate < DateTime.Now && 
  13:                  terminateDate > hireDate)
  14:              {
  15:                  // do awesome stuff
  16:                  return true;
  17:              }
  18:   
  19:              return false;
  20:          }
  21:      }
Right-click the method

image

Extract Class From Parameters

SNAGHTML10f0d4a5

After
   1:      public class EmployeeInfo
   2:      {
   3:          private int _employeeId;
   4:          private string _firstName;
   5:          private string _lastName;
   6:          private DateTime _hireDate;
   7:          private DateTime _terminateDate;
   8:   
   9:          public EmployeeInfo(int employeeId, string firstName, string lastName, DateTime hireDate, DateTime terminateDate)
  10:          {
  11:              _employeeId = employeeId;
  12:              _firstName = firstName;
  13:              _lastName = lastName;
  14:              _hireDate = hireDate;
  15:              _terminateDate = terminateDate;
  16:          }
  17:   
  18:          public int EmployeeId
  19:          {
  20:              get { return _employeeId; }
  21:          }
  22:   
  23:          public string FirstName
  24:          {
  25:              get { return _firstName; }
  26:          }
  27:   
  28:          public string LastName
  29:          {
  30:              get { return _lastName; }
  31:          }
  32:   
  33:          public DateTime HireDate
  34:          {
  35:              get { return _hireDate; }
  36:          }
  37:   
  38:          public DateTime TerminateDate
  39:          {
  40:              get { return _terminateDate; }
  41:          }
  42:      }
  43:   
  44:      public class EmployeeUtilities
  45:      {
  46:          public bool ValidateTerminatedEmployeeInfo(EmployeeInfo employeeInfo)
  47:          {
  48:              if (employeeInfo.EmployeeId > -1 && 
  49:                  !String.IsNullOrWhiteSpace(employeeInfo.FirstName) && 
  50:                  !String.IsNullOrWhiteSpace(employeeInfo.LastName) &&
  51:                  employeeInfo.HireDate < DateTime.Now && 
  52:                  employeeInfo.TerminateDate > employeeInfo.HireDate)
  53:              {
  54:                  // do awesome stuff
  55:                  return true;
  56:              }
  57:   
  58:              return false;
  59:          }
  60:      }

Happy coding!

 

C# + ReSharper = Awesome: Tip #7–Move String to Resource

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

Tip #7 – Move String to Resource

Use: Moves a string into a resource file to enable localization.

Tip: Ensure your project contains at least one Resource (resx) file or the operation will fail.

Before
   1:       public string YouRock()
   2:       {
   3:           return "No, YOU rock Mr. Method-Caller!";
   4:       }
Right-click the string –> Refactor –> Refactor This…

image

Move to Resource

SNAGHTML6f7080

After
   1:       public string YouRock()
   2:       {
   3:           return StringResources.No_YOU_rock_Mr_Method_Caller;
   4:       }

Happy coding!