I’m trying to compare two IList<T> by their Type. Both lists have the same T and therefore I thought they should have the same type.
In debug-mode in Visual Studio in the tooltip I can read the types of both and it is the same.
But Equals() ant the == Operator return both false.
Can anyone explanin this weired behavior?
Little Example:
class Program
{
static void Main(string[] args)
{
IList<string> list1 = new List<string>();
IList<string> list2 = new List<string>();
var type1 = list1.GetType();
var type2 = typeof(IList<string>);
if (type1.Equals(type2))
{
Console.WriteLine("equal");
}
else
{
Console.WriteLine("non equal");
}
Console.ReadLine();
}
}
==> non equal
Edit:
I coose a bad Example, this one shows the way I was trying to to it.
I’m using .Net 3.5
Yes, you’re comparing two types:
List<string>andIList<string>. They’re not the same type, and I don’t know why you’d expect them to be the same.It’s unclear what you’re trying to do, but you might want to use
Type.IsAssignableFrom. For example, in your example,will print True.
Answer from before the edit…
Unable to reproduce:
Is it possible that in your real code, they’re both implementations of
IList<string>, but different implementations, e.g.That will show the types being different, because one is a
List<string>and the other is astring[].