Eric J.
Eric J.

Reputation: 150148

Express C# generic type arguments more succinctly?

Inspired by

Comparing two collections for equality irrespective of the order of items in them

I created an extension method to test whether two collections are equivalent. To use the extension method, I have to specify two type parameters like this:

IsEquivalentToTestHelper<ObservableCollection<string>, string>(first, second, true);

Is there a way to implement the extension method so that only one generic constraint (e.g. ObservableCollection<string>) need be specified?

UPDATE: Posted the modified code based on answers as an additional response to the question that inspired this one.

Here's my original code:

static public class EnumerableExtensions 
{
    static public bool IsEquivalentTo<E,T>(this E first, E second) where E : IEnumerable<T>
    {
        if ((first == null) != (second == null))
            return false;

        if (!object.ReferenceEquals(first, second) && (first != null))
        {
            if (first.Count() != second.Count())
                return false;

            if ((first.Count() != 0) && HaveMismatchedElement<E,T>(first, second))
                return false;
        }

        return true;
    }

    private static bool HaveMismatchedElement<E,T>(E first, E second) where E : IEnumerable<T>
    {
        int firstCount;
        int secondCount;

        var firstElementCounts = GetElementCounts<E,T>(first, out firstCount);
        var secondElementCounts = GetElementCounts<E,T>(second, out secondCount);

        if (firstCount != secondCount)
            return true;

        foreach (var kvp in firstElementCounts)
        {
            firstCount = kvp.Value;
            secondElementCounts.TryGetValue(kvp.Key, out secondCount);

            if (firstCount != secondCount)
                return true;
        }

        return false;
    }

    private static Dictionary<T, int> GetElementCounts<E,T>(E enumerable, out int nullCount) where E : IEnumerable<T>
    {
        var dictionary = new Dictionary<T, int>();
        nullCount = 0;

        foreach (T element in enumerable)
        {
            if (element == null)
            {
                nullCount++;
            }
            else
            {
                int num;
                dictionary.TryGetValue(element, out num);
                num++;
                dictionary[element] = num;
            }
        }

        return dictionary;
    }

    static private int GetHashCode<E,T>(IEnumerable<T> enumerable) where E : IEnumerable<T>
    {
        int hash = 17;

        foreach (T val in enumerable.OrderBy(x => x))
            hash = hash * 23 + val.GetHashCode();

        return hash;
    }
}

Upvotes: 0

Views: 210

Answers (3)

Osguima3
Osguima3

Reputation: 151

You just need to replace every E with IEnumerable<T>, and drop the where statements

For example:

static public bool IsEquivalentTo<T>(this IEnumerable<T> first, IEnumerable<T> second)

var firstElementCounts = GetElementCounts<IEnumerable<T>,T>(first, out firstCount);

static private int GetHashCode<T>(IEnumerable<T> enumerable)

Upvotes: 1

the_joric
the_joric

Reputation: 12261

static public bool IsEquivalentTo<T>(this IEnumerable<T> first, IEnumerable<T> second) 

Upvotes: 4

zmbq
zmbq

Reputation: 39033

You could drop the first one and keep the second:

static public bool IsEquivalentTo<T>(this IEnumerable<T> first, IEnumerable<T> second)

Upvotes: 2

Related Questions