Skip to content

Instantly share code, notes, and snippets.

@jonsagara
Created May 7, 2011 21:02
Show Gist options
  • Save jonsagara/960834 to your computer and use it in GitHub Desktop.
Save jonsagara/960834 to your computer and use it in GitHub Desktop.
Sample immutable struct that shows how to override Equals and GetHashCode, as well as the == and != operators
/// <summary>
/// Sample struct that shows how to override GetHashCode and Equals, as well as the == and !=
/// operators.
/// </summary>
/// <remarks>
/// This type is immutable.
/// </remarks>
public struct SampleStruct
{
#region Private Backing Fields
private readonly DateTime _startDate;
private readonly DateTime _endDate;
#endregion
#region Public Properties
public DateTime StartDate { get { return this._startDate; } }
public DateTime EndDate { get { return this._endDate; } }
#endregion
#region Constructors
public SampleStruct(DateTime startDate, DateTime endDate)
: this()
{
this._startDate = startDate;
this._endDate = endDate;
}
#endregion
#region Override Methods
public override string ToString()
{
return string.Format("{0} - {1}", this.StartDate, this.EndDate);
}
public override int GetHashCode()
{
// Overflow is fine, just wrap
unchecked
{
int hash = 17;
hash = hash * 23 + this.StartDate.GetHashCode();
hash = hash * 23 + this.EndDate.GetHashCode();
return hash;
}
}
public override bool Equals(object obj)
{
if (object.ReferenceEquals(null, obj))
{
return false;
}
if (object.ReferenceEquals(this, obj))
{
return true;
}
if (obj.GetType() != typeof(SampleStruct))
{
return false;
}
return this.Equals((SampleStruct)obj);
}
public bool Equals(SampleStruct other)
{
if (object.ReferenceEquals(null, other))
{
return false;
}
if (object.ReferenceEquals(this, other))
{
return true;
}
return this.StartDate == other.StartDate
&& this.EndDate == other.EndDate;
}
public static bool operator ==(SampleStruct left, SampleStruct right)
{
return object.Equals(left, right);
}
public static bool operator !=(SampleStruct left, SampleStruct right)
{
return !object.Equals(left, right);
}
#endregion
}
class Program
{
static void Main(string[] args)
{
var now = DateTime.Now;
var s1 = new SampleStruct(now, now.AddDays(1.0));
var s2 = new SampleStruct(now, now.AddDays(1.0));
Console.WriteLine("s1 == s1: {0}", s1 == s1);
Console.WriteLine("s1 == s2: {0}", s1 == s2);
Console.WriteLine("s2 == s1: {0}", s2 == s1);
Console.WriteLine("s2 == s2: {0}", s2 == s2);
Console.WriteLine();
Console.WriteLine("s1 != s1: {0}", s1 != s1);
Console.WriteLine("s1 != s2: {0}", s1 != s2);
Console.WriteLine("s2 != s1: {0}", s2 != s1);
Console.WriteLine("s2 != s2: {0}", s2 != s2);
Console.WriteLine();
Console.WriteLine("s1.Equals(s1): {0}", s1.Equals(s1));
Console.WriteLine("s1.Equals(s2): {0}", s1.Equals(s2));
Console.WriteLine("s2.Equals(s1): {0}", s2.Equals(s1));
Console.WriteLine("s2.Equals(s2): {0}", s2.Equals(s2));
Console.WriteLine();
Console.WriteLine("!s1.Equals(s1): {0}", !s1.Equals(s1));
Console.WriteLine("!s1.Equals(s2): {0}", !s1.Equals(s2));
Console.WriteLine("!s2.Equals(s1): {0}", !s2.Equals(s1));
Console.WriteLine("!s2.Equals(s2): {0}", !s2.Equals(s2));
Console.WriteLine();
Console.WriteLine("object.ReferenceEquals(s1, s1): {0}", object.ReferenceEquals(s1, s1));
Console.WriteLine("object.ReferenceEquals(s1, s2): {0}", object.ReferenceEquals(s1, s2));
Console.WriteLine("object.ReferenceEquals(s2, s1): {0}", object.ReferenceEquals(s2, s1));
Console.WriteLine("object.ReferenceEquals(s2, s2): {0}", object.ReferenceEquals(s2, s2));
Console.WriteLine();
Console.WriteLine("!object.ReferenceEquals(s1, s1): {0}", !object.ReferenceEquals(s1, s1));
Console.WriteLine("!object.ReferenceEquals(s1, s2): {0}", !object.ReferenceEquals(s1, s2));
Console.WriteLine("!object.ReferenceEquals(s2, s1): {0}", !object.ReferenceEquals(s2, s1));
Console.WriteLine("!object.ReferenceEquals(s2, s2): {0}", !object.ReferenceEquals(s2, s2));
Console.WriteLine("Press ENTER to quit...");
Console.ReadLine();
}
}
/* Output:
s1 == s1: True
s1 == s2: True
s2 == s1: True
s2 == s2: True
s1 != s1: False
s1 != s2: False
s2 != s1: False
s2 != s2: False
s1.Equals(s1): True
s1.Equals(s2): True
s2.Equals(s1): True
s2.Equals(s2): True
!s1.Equals(s1): False
!s1.Equals(s2): False
!s2.Equals(s1): False
!s2.Equals(s2): False
object.ReferenceEquals(s1, s1): False
object.ReferenceEquals(s1, s2): False
object.ReferenceEquals(s2, s1): False
object.ReferenceEquals(s2, s2): False
!object.ReferenceEquals(s1, s1): True
!object.ReferenceEquals(s1, s2): True
!object.ReferenceEquals(s2, s1): True
!object.ReferenceEquals(s2, s2): True
Press ENTER to quit...
*/
@jonsagara
Copy link
Author

I should note that some of this code comes from various places:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment