It’s a good rule of thumb to overload the equality operators on classes. That ensures a correct comparison between to class instances of the same type. If you don’t, .NET automatically uses reflection and that is way slower than a custom implementation.

We all use the equality operators (“==”, “!=”) all the time and we expect them to be right every time. They are not!, but even if they were there is a good chance that you want to change them anyway. For instance, if your class has a unique Id property, then that property is the one that tells if two instances are the same. If you don’t overload the operators, you could have two instances with the same Id treated as two different instances.

First of all, you have to find out what makes two instances different. Let’s keep using the Id property example in an imaginary class called Foo. Then lets start overriding the GetHashCode() method so it returns the hashcode of the Id property.

/// <summary>

/// A uniquely key to identify this particullar instance of the class

/// </summary>

/// <returns>A unique integer value</returns>

public override int GetHashCode()

{

  return this.Id.GetHashCode();

}

Whenever the GetHashCode() method is called, it returns a unique integer value that represents the uniqueness of the class instance. Now we can use that method to overload the operators.

/// <summary>

/// Checks to see if two business objects are the same.

/// </summary>

public static bool operator ==(Foo first, Foo second)

{

  // Checks if the pointer in memory is the same

  if (Object.ReferenceEquals(first, second))

  {

    return true;

  }

 

  // Checks for null values

  if ((object)first == null || (object)second == null)

  {

    return false;

  }

 

  return first.GetHashCode() == second.GetHashCode();

}

 

/// <summary>

/// Checks to see if two business objects are different.

/// </summary>

public static bool operator !=(Foo first, Foo second)

{

  return !(first == second);

}

The reason why it is necessary to box the two Foo parameters (first, second) to type object, is to avoid an overflow exception.
 
We still need one more thing to be sure that we can compare two instances to each other, and that is by overriding the Equals() method. You can use the Equals() method or the operators to compare instances and they should always return the same result.

/// <summary>
///
Comapares this object with another
///
</summary>
///
<param name="obj">The object to compare</param>
///
<returns>True if the two objects as equal</returns>
public
override bool Equals(object obj)
{
  if (obj == null)
  {
    return false;
  }

  if (obj.GetType() == this.GetType())
  {
    return obj.GetHashCode() == this.GetHashCode();
 
}

  return false;
}

You can implement these methods on a base class so you won’t have to write it on all the derived classes. Because these methods uses GetHashCode() to compare two instances, it would be preferable to override that method in the derived classes if needed.

Whenever you write class libraries, custom control or just about anything else, you probably raise a lot of home made events. That’s a simple thing to do, but tedious to write over and over again.

That’s why I always use this snippet for Visual Studio 2005 that writes the whole thing for me. Download the snippet and place it in the Visual Studio snippet folder which is placed at My documents\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets\.

Just fill out the name of the event and the EventArgs type that by default is EventArgs. It will then write the XML comment and a method that raises the event in a safe manor. That method is given the same name as the event, but prefixed “On”. So, if you create an event called Saved, the method that raises the event is called OnSaved.

This is what happens when you type evnt and hit TAB twice.

 

And after you’ve filled out the type of EventArgs and named the event Saved, this will be the result:

  public event EventHandler<EventArgs> Saved;

  /// <summary>

  /// Occurs when the class is Saved

  /// </summary>

  protected void OnSaved()

  {

    if (Saved != null)

    {

      Saved(this, new EventArgs());

    }

  }

This saves me for typing the same stuff over and over again.

For more snippets, visit Vault of Thoughts who has some nice ones.