Easy collection comparing in C#

Copmare collections in C# the easy way

  • Share

Collections are part of pretty much any application and they are convenient to store any type of data. Introducing LINQ in .NET things got a lot easier as your code does not need to be full with loops for performing simple operations on collections of any type. However some simple operations like equality regardless of the item position in collection are not part of every collection in .NET.

One useful class introduced in .NET framework is HashSet. This type of collection has implemented various method for comparing the collections of same type. This is nice to use in case you are writing your application from the scratch, but in case you have application with dozens of different collections, things get messy when you want to perform some of the operations available in HashSet collections.

This is the case for using extension methods to extend generic collection functionalities. Below are three methods that I found very useful in collection operations which are basically using HashSets under the hood.

using System.Collections.Generic;
using System.Linq;

namespace MultiConsoleOutput
{
    public static class ExtensionMethods
    {
        public static bool IsEqual<T>(this IEnumerable<T> comparing, IEnumerable<T> compareTo)
        {
            if (comparing.Count() == compareTo.Count())
            {
                return new HashSet<T>(comparing).Intersect<T>(compareTo).Any();
            }
            return false;
        }

        public static bool Contains<T>(this IEnumerable<T> comparing, IEnumerable<T> compareTo)
        {
            return new HashSet<T>(comparing).IsSupersetOf(compareTo);
        }

        public static bool ContainedIn<T>(this IEnumerable<T> comparing, IEnumerable<T> compareTo)
        {
            return new HashSet<T>(comparing).IsSubsetOf(compareTo);
        }
    }
}

    

To demonstrate functionality of these methods lets take IsEqual extension method which uses the HashSet to compare two collections regardless of the element position in collection.

            List<int> list1 = new List<int>() { 1, 2, 3, 4, 5, 6 };
            List<int> list2 = new List<int>() { 2, 3, 6, 5, 1, 4 };

            Console.WriteLine(list1.Equals(list2)); //False
            Console.WriteLine(list1.IsEqual(list2)); //True
    

You can see that Equals method returned result False while IsEqual returns True. Basically these are two same collections, just with different element order. The same goes for Contains method for collection which only accepts one parameter of the same type to determine whether collection contains element while Contains extension method accepts collection which will use for comparing by translating collection to HashSet under the hood.

References

  • Share

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

Comments for this article

comments powered by Disqus