Checking Object Equality in C#

If you want to check if one instance of a custom class is equal to another instance of it you need to override the Equals operator for the class and implement your own.

The code below is based on the articles The Right Way to do Equality in C# and Overriding Equals in C# (Part 2).


public class ResourceAssignment : IEquatable<ResourceAssignment>
{
	public int ResourceId { get; set; }

	public int? ParentResourceId { get; set; }

	public int DayId { get; set; }

	public string Week { get; set; }

	public int ClientId { get; set; }

	public int TaskTypeId { get; set; }

	public int TaskId { get; set; }

	public int AssignmentId { get; set; }

	public int PersonId { get; set; }

	public double? Hours { get; set; }

	public int StatusId { get; set; }

	public bool IsDeleted { get; set; }

	public bool Equals(ResourceAssignment other)
	{
		if (other == null)
		{
			return false;
		}

		return ResourceId == other.ResourceId
			&& ParentResourceId.Equals(other.ParentResourceId)
			&& DayId == other.DayId
			&& string.Equals(Week, other.Week)
			&& ClientId == other.ClientId
			&& TaskTypeId == other.TaskTypeId
			&& TaskId == other.TaskId
			&& AssignmentId == other.AssignmentId
			&& PersonId == other.PersonId
			&& Hours.Equals(other.Hours)
			&& StatusId == other.StatusId
			&& IsDeleted == other.IsDeleted;
	}

	public override bool Equals(object obj)
	{
		if (ReferenceEquals(null, obj)) return false;
		if (ReferenceEquals(this, obj)) return true;
		if (obj.GetType() != GetType()) return false;
		return Equals(obj as ResourceAssignment);
	}

	public override int GetHashCode()
	{
		unchecked
		{
			// Choose large primes to avoid hashing collisions
			const int HashingBase = (int)2166136261;
			const int HashingMultiplier = 16777619;

			int hash = HashingBase;
			hash = (hash * HashingMultiplier) ^ (!Object.ReferenceEquals(null, ResourceId) ? ResourceId.GetHashCode() : 0);
			hash = (hash * HashingMultiplier) ^ (!Object.ReferenceEquals(null, ParentResourceId) ? ParentResourceId.GetHashCode() : 0);
			hash = (hash * HashingMultiplier) ^ (!Object.ReferenceEquals(null, DayId) ? DayId.GetHashCode() : 0);
			hash = (hash * HashingMultiplier) ^ (!Object.ReferenceEquals(null, Week) ? Week.GetHashCode() : 0);
			hash = (hash * HashingMultiplier) ^ (!Object.ReferenceEquals(null, ClientId) ? ClientId.GetHashCode() : 0);
			hash = (hash * HashingMultiplier) ^ (!Object.ReferenceEquals(null, TaskTypeId) ? TaskTypeId.GetHashCode() : 0);
			hash = (hash * HashingMultiplier) ^ (!Object.ReferenceEquals(null, TaskId) ? TaskId.GetHashCode() : 0);
			hash = (hash * HashingMultiplier) ^ (!Object.ReferenceEquals(null, AssignmentId) ? AssignmentId.GetHashCode() : 0);
			hash = (hash * HashingMultiplier) ^ (!Object.ReferenceEquals(null, PersonId) ? PersonId.GetHashCode() : 0);
			hash = (hash * HashingMultiplier) ^ (!Object.ReferenceEquals(null, Hours) ? Hours.GetHashCode() : 0);
			hash = (hash * HashingMultiplier) ^ (!Object.ReferenceEquals(null, StatusId) ? StatusId.GetHashCode() : 0);
			hash = (hash * HashingMultiplier) ^ (!Object.ReferenceEquals(null, IsDeleted) ? IsDeleted.GetHashCode() : 0);
			return hash;
		}
	}
}

This is then used in the below function to check if an instance of ResourceAssignment exists in a list of ResourceAssignment and if it doesn’t to run a SQL stored procedure to create a new entry for the provided ResourceAssignment.


public static ResourceAssignment CheckResourceAssignment(ResourceAssignment resourceAssignment, List<ResourceAssignment> resourceAssignments, string connectionString)
{
	// First check to see if exists in current list
	if (resourceAssignments.Where(x => x.Equals(resourceAssignment)).Count() == 0)
	{
		// If doesn't already exist then create in SQL
		resourceAssignment = CreateResourceAssignment(resourceAssignment, connectionString);
	}

	return resourceAssignment;
}

Leave a Reply

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