user2079647
user2079647

Reputation: 11

Compare 2 List<> without using Linq

Im working with 2 List, i want to see if the main contains the same types. The 2 lists do not need to contain the same count or order, just have all of the matching Types. I know this is very possible with Linq, however i cannot use that.

    private static bool ContentsMatch(List<Type> list1, List<Type> list2)
    {
        if (list1.Count != list2.Count)
            return false;

        for (int i = 0; i < list1.Count; i++)
        {
            if (!list1[i].Equals(list2[i]))
                return false;
        }
        return true;
    }

The above method i tried will only return true if they are in the same order.

Upvotes: 1

Views: 249

Answers (4)

leppie
leppie

Reputation: 117310

Code for algorithm provided in the comments.

Does not depend on order or count or duplicate items. Also generic and abstracted.

bool IsSameSet<T>(IEnumerable<T> l1, IEnumerable<T> l2)
{
  return IsSubSet(l1, l2) && IsSubSet(l2, l1); 
}

bool IsSubSet<T>(IEnumerable<T> l1, IEnumerable<T> l2)
{
  var lookup = new Dictionary<T, bool>();

  foreach (var e in l1)
    lookup[e] = true;

  foreach (var e in l2)
    if (!lookup.ContainsKey(e))
      return false;

  return true;
}

Usage:

Type[] l1 = { typeof(object), typeof(int), typeof(long), typeof(object) };
Type[] l2 = { typeof(int), typeof(long), typeof(object) };

var result = IsSameSet(l1, l2);
Console.WriteLine(result);  // prints true

Exercise for the user:

Add an additional parameter to provide an IEqualityComparer<T> to be passed to the dictionary.

Upvotes: 3

Sreekar Kothamachu
Sreekar Kothamachu

Reputation: 64

To compare any user defined customized types, we need to override Equals & GetHashCode. Below is the code snippet you could refer to :

    public class CustomizedDataType
    {
        private int field1;
        private string field2;

        public CustomizedDataType(int field1,string field2)
        {
            this.field1 = field1;
            this.field2 = field2;
        }

        public override bool Equals(object obj)
        {
            CustomizedDataType dataType = obj as CustomizedDataType;
            if (this.field1 == dataType.field1 && this.field2 == dataType.field2)
            {
                return true;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return (this.field1.GetHashCode() + this.field2.GetHashCode());
        }

Sample code to execute :

    static void Main(string[] args)
    {
        //Test Data
        List<CustomizedDataType> dataTypeContaineer1 = new List<CustomizedDataType>();
        dataTypeContaineer1.Add(new CustomizedDataType(10,"Test10"));
        dataTypeContaineer1.Add(new CustomizedDataType(11, "Test11"));
        dataTypeContaineer1.Add(new CustomizedDataType(12, "Test12"));

        //Test Data
        List<CustomizedDataType> dataTypeContaineer2 = new List<CustomizedDataType>();
        dataTypeContaineer2.Add(new CustomizedDataType(100, "Test10"));
        dataTypeContaineer2.Add(new CustomizedDataType(11, "Test11"));
        dataTypeContaineer2.Add(new CustomizedDataType(12, "Test120"));

        //Checking if both the list contains the same types.
        if (dataTypeContaineer1.GetType() == dataTypeContaineer2.GetType())
        {
            //Checking if both the list contains the same count
            if (dataTypeContaineer1.Count == dataTypeContaineer2.Count)
            {
                //Checking if both the list contains the same data.
                for (int index = 0; index < dataTypeContaineer1.Count; index++)
                {
                    if(!dataTypeContaineer1[index].Equals(dataTypeContaineer2[index]))
                    {
                        Console.WriteLine("Mismatch @ Index {0}", index);
                    }
                }
            }
        }
    }

Output :

enter image description here

Upvotes: 0

Brad Christie
Brad Christie

Reputation: 101614

Assuming you mean that that two List<T> both have matching T, you could use:

private static Boolean MatchingBaseType(IEnumerable a, IEnumerable b)
{
    return GetIListBaseType(a) == GetIListBaseType(b);
}

private static Type GetIListBaseType(IEnumerable a)
{
    foreach (Type interfaceType in a.GetType().GetInterfaces())
    {
        if (interfaceType.IsGenericType &&
            (interfaceType.GetGenericTypeDefinition() == typeof(IList<>) ||
             interfaceType.GetGenericTypeDefinition() == typeof(IEnumerable<>) ||
             interfaceType.GetGenericTypeDefinition() == typeof(ICollection<>))
        )
        {
            return interfaceType.GetGenericArguments()[0];
        }
    }
    return default(Type);
}

You say count doesn't matter (though you're checking .Count()--why?) But this should return if the two lists have the same types in them.

Upvotes: -1

SpartyOn
SpartyOn

Reputation: 24

You can use the C# keyword 'is' to see if an object is compatible with a given type. http://msdn.microsoft.com/en-us/library/vstudio/scekt9xw.aspx

Upvotes: -1

Related Questions