117
public bool IsList(object value)
    {
        Type type = value.GetType();
        // Check if type is a generic list of any type
    }

What's the best way to check if the given object is a list, or can be cast to a list?

1

9 Answers 9

159

For you guys that enjoy the use of extension methods:

public static bool IsGenericList(this object o)
{
    var oType = o.GetType();
    return (oType.IsGenericType && (oType.GetGenericTypeDefinition() == typeof(List<>)));
}

So, we could do:

if(o.IsGenericList())
{
 //...
}
3
  • 6
    For .Net Core this needs to be modified slightly to return oType.GetTypeInfo().IsGenericType && oType.GetGenericTypeDefinition() == typeof(List<>);
    – Rob L
    Jun 10, 2017 at 11:28
  • Works like a charm! If you only have the type not the object this will work for you! Thanks!!
    – gatsby
    Apr 5, 2018 at 15:31
  • Would checking for IList<> instead be safer?
    – nl-x
    Dec 12, 2019 at 18:09
121
using System.Collections;

if(value is IList && value.GetType().IsGenericType) {

}
7
  • 6
    This does not work - I get the following exception - value is IList Using the generic type 'System.Collections.Generic.IList<T>' requires '1' type arguments
    – Jason
    Apr 27, 2009 at 16:38
  • 20
    You need to add using System.Collections; on top of your source file. The IList interface I suggested is NOT the generic version (hence the second check) Apr 27, 2009 at 16:41
  • 1
    You're right. This works like a charm. I was testing this in my Watch window and forgot all about the missing namespace. I like this solution better, very simple
    – Jason
    Apr 27, 2009 at 16:51
  • 3
    This doesn't work. I would guess in 4.0 IList<T> != IList? Anyway, I had to check if it was generic and IEnumerable, and then check for the existence of the property I wanted to check, "Count". I suppose this weakness is partly why WCF turns all of your List<T>'s into T[].
    – user1086498
    May 9, 2013 at 20:55
  • 1
    @Edza Incorrect. This usually works since List<T> and ObservableCollection<T> implement IList.
    – HappyNomad
    Jul 27, 2016 at 22:16
17
 bool isList = o.GetType().IsGenericType 
                && o.GetType().GetGenericTypeDefinition() == typeof(IList<>));
8

Here's an implementation that works in .NET Standard, and works against interfaces:

    public static bool ImplementsGenericInterface(this Type type, Type interfaceType)
    {
        return type
            .GetTypeInfo()
            .ImplementedInterfaces
            .Any(x => x.GetTypeInfo().IsGenericType && x.GetGenericTypeDefinition() == interfaceType);
    }

And here are the tests (xunit):

    [Fact]
    public void ImplementsGenericInterface_List_IsValidInterfaceTypes()
    {
        var list = new List<string>();
        Assert.True(list.GetType().ImplementsGenericInterface(typeof(IList<>)));
        Assert.True(list.GetType().ImplementsGenericInterface(typeof(IEnumerable<>)));
        Assert.True(list.GetType().ImplementsGenericInterface(typeof(IReadOnlyList<>)));
    }

    [Fact]
    public void ImplementsGenericInterface_List_IsNotInvalidInterfaceTypes()
    {
        var list = new List<string>();
        Assert.False(list.GetType().ImplementsGenericInterface(typeof(string)));
        Assert.False(list.GetType().ImplementsGenericInterface(typeof(IDictionary<,>)));
        Assert.False(list.GetType().ImplementsGenericInterface(typeof(IComparable<>)));
        Assert.False(list.GetType().ImplementsGenericInterface(typeof(DateTime)));
    }
7
public bool IsList(object value) {
    return value is IList 
        || IsGenericList(value);
}

public bool IsGenericList(object value) {
    var type = value.GetType();
    return type.IsGenericType
        && typeof(List<>) == type.GetGenericTypeDefinition();
}
6

Based on Victor Rodrigues' answer, we can devise another method for generics. In fact, the original solution can be reduced to only two lines:

public static bool IsGenericList(this object Value)
{
    var t = Value.GetType();
    return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>);
}

public static bool IsGenericList<T>(this object Value)
{
    var t = Value.GetType();
    return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<T>);
}
6

I'm using the following code:

public bool IsList(Type type) => type.IsGenericType && (
            (type.GetGenericTypeDefinition() == typeof(List<>))
            || (type.GetGenericTypeDefinition() == typeof(IList<>))
            );
5
if(value is IList && value.GetType().GetGenericArguments().Length > 0)
{

}
1
  • I think you need a call to GetType() e.g. value.GetType().GetGenericArguments().Length > 0
    – ScottS
    Apr 27, 2009 at 16:17
1

Probably the best way would be to do something like this:

IList list = value as IList;

if (list != null)
{
    // use list in here
}

This will give you maximum flexibility and also allow you to work with many different types that implement the IList interface.

1
  • 3
    this does not check if it a generic list as asked.
    – Lucas
    Apr 27, 2009 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.