.Net C# using the IEquatable interface

A quick demonstration of the .Net IEquatable interface, inspired by MSDN. It’s used to support testing of object type equality.

using System;
using System.Collections.Generic;

namespace CsharpIEquitableDemo
{
    public class IEquatableExample
    {
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        static void Main()
        {
            // Create a TestClass object
            var testCaseOne = new TestClass("Jones", "000001");
            var testCaseTwo = new TestClass("Smith", "000002");
            var testCaseThree = new TestClass("Jones", "000003");
            var testCaseFour = new TestClass("Wilson", "000004");

            // Add test data to a List object.
            var testCases = new List<TestClass> { testCaseOne, testCaseTwo, testCaseThree, testCaseFour };

            // Create a TestClass object for test
            var testCase = new TestClass("Jones", "000001");

            // test if object is found
            if (!testCases.Contains(testCase))
                Console.WriteLine("Test case {0} not found.", testCase.ParamTwo);
            else
                Console.WriteLine("Test case {0} ParamTwo {1} found.",
                                  testCase.ParamOne, testCase.ParamTwo);

            Console.WriteLine("Does Test case {0} {1} already exist? : {2} ",
                              testCaseTwo.ParamOne,
                              testCaseTwo.ParamTwo,
                              Equals(testCaseTwo, testCase));

            Console.ReadKey();
        }
    }

    /// <summary>
    /// Class to demonstrate the IEquatable interface
    /// </summary>
    public class TestClass : IEquatable<TestClass>
    {
        public string ParamOne { get; private set; }

        public string ParamTwo { get; private set; }

        public TestClass(string testParamOne, string testParamTwo)
        {
            ParamOne = testParamOne;
            ParamTwo = testParamTwo;
        }

        /// <summary>
        /// Tests equality of the specified input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        public bool Equals(TestClass input)
        {
            return input != null && ParamTwo == input.ParamTwo;
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object" /> is equal to the current <see cref="T:System.Object" />.
        /// </summary>
        /// <param name="obj">The object to compare with the current object.</param>
        /// <returns>
        /// true if the specified object  is equal to the current object; otherwise, false.
        /// </returns>
        public override bool Equals(Object obj)
        {
            if (obj != null)
            {
                var testClassObj = obj as TestClass;
                return testClassObj != null && Equals(testClassObj);
            }

            return false;
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
        /// </returns>
        public override int GetHashCode()
        {
            return ParamTwo.GetHashCode();
        }

        /// <summary>
        /// Test for object equality.
        /// </summary>
        /// <param name="testParamOne">The test parameter one.</param>
        /// <param name="testParamTwo">The test parameter two.</param>
        /// <returns></returns>
        public static bool operator ==(TestClass testParamOne, TestClass testParamTwo)
        {
            return (object)testParamOne == null || ((object)testParamTwo) == null
                       ? Equals(testParamOne, testParamTwo)
                       : testParamOne.Equals(testParamTwo);
        }

        /// <summary>
        /// Test for object inequality.
        /// </summary>
        /// <param name="testParamOne">The test parameter one.</param>
        /// <param name="testParamTwo">The test parameter two.</param>
        /// <returns></returns>
        public static bool operator !=(TestClass testParamOne, TestClass testParamTwo)
        {
            return testParamOne == null || testParamTwo == null
                       ? !Equals(testParamOne, testParamTwo)
                       : !(testParamOne.Equals(testParamTwo));
        }
    }
}

Running the code returns:

Test case Jones ParamTwo 000001 found.
Does Test case Smith 000002 already exist? : False

Leave a Reply

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