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

By | February 19, 2012

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!

 

3 thoughts on “C# + ReSharper = Awesome: Tip #10 of 10 – Generate Equality Members

  1. Rachel @ DevExpress

    Hi Alvin,

    Nice series! Very useful for all who are reading.

    Once you get to looking at CodeRush, drop us a line. I know that Rory (@RoryBecker) will be glad to help you out with any questions you might have, or show you some of his favourite tricks too.

    Take care,
    Rachel.
    (@RachelHawley)

    Reply
  2. James Pansarasa

    Thanks for the series but just a quick couple of notes:

    People should be take care when implementing equals and the distinction between value types and reference types

    Microsoft has guidelines for implementing in the “Design Guidelines for Developing Class Libraries”
    http://msdn.microsoft.com/en-us/library/7h9bszxx(v=vs.100).aspx

    For value types, you should override the equality operator.
    Most reference types, even those that implement the Equals method, should not override the equality operator (==).

    Also the implementation of GetHashCode() is greatly simplified in your example and not great “out of the box” for generating hashes with more than one participant

    public override int GetHashCode()
    {
    unchecked
    {
    int result = OrderId;
    result = (result*397) ^ CustomerId;
    result = (result*397) ^ OrderDate.GetHashCode();
    return result;
    }
    }

    For a better hash code I would look to a modified FNV or a murmur hash implementation which would give a better distribution.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *