Dew Drop – February 28, 2017 (#2431)

Top Links

 

Web Development

 

XAML

 

Miscellaneous .NET

 

Design / Methodology / Testing

 

Other Mobile Platforms

 

Podcasts / Screencasts / Videos

 

Community / Events

 

Database

 

SharePoint

 

Miscellaneous

 

More Link Collections

 

The Geek Shelf

Dew Drop – April 11, 2012 (#1,304)

Top Links

 

.NET / Visual Studio

 

Web Development

 

Design / Methodology / Testing

 

Silverlight / WPF / Windows Phone

 

Podcasts / Screencasts / Videos

 

Community / Events

 

Database

 

SharePoint

 

PowerShell

 

Miscellaneous

 

More Link Collections

 

The Geek Shelf

Pro ASP.NET MVC 3 Framework by Steven Sanderson

 

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!

 

Mastodon
github.com/alvinashcraft