Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8021239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:55:22+00:00 2026-06-04T21:55:22+00:00

I am trying to determine if a runtime type is some sort of collection

  • 0

I am trying to determine if a runtime type is some sort of collection type. What I have below works, but it seems strange that I have to name the types that I believe to be collection types in an array like I have done.

In the code below, the reason for the generic logic is because, in my app, I expect all collections to be generic.

bool IsCollectionType(Type type)
{
    if (!type.GetGenericArguments().Any())
        return false;

    Type genericTypeDefinition = type.GetGenericTypeDefinition();
    var collectionTypes = new[] { typeof(IEnumerable<>), typeof(ICollection<>), typeof(IList<>), typeof(List<>) };
    return collectionTypes.Any(x => x.IsAssignableFrom(genericTypeDefinition));
}

How would I refactor this code to be smarter or simpler?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:55:22+00:00Added an answer on June 4, 2026 at 9:55 pm

    Really all of these types inherit IEnumerable. You can check only for it:

    bool IsEnumerableType(Type type)
    {
        return (type.GetInterface(nameof(IEnumerable)) != null);
    }
    

    or if you really need to check for ICollection:

    bool IsCollectionType(Type type)
    {
        return (type.GetInterface(nameof(ICollection)) != null);
    }
    

    Look at "Syntax" part:

    • List<T>

    • IList

    • ICollection

    If you need to exclude strings (which are essentially an IEnumerable<char>), use the following function:

    bool IsEnumerableType(Type type)
    {
        return (type.Name != nameof(String) 
            && type.GetInterface(nameof(IEnumerable)) != null);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: MySQL: determine which database is selected? I'm trying to debug some problems
I'm trying to publish a web site . The publication works perfectly, but when
I am searching for a way to determine at runtime, which type of object
I am trying to determine the height of a Spark label that becomes multiline
I'm trying to optimize some code where I have a large number of arrays
In trying to determine the precise order of fields in a .NET value type,
Trying to determine if it is possible to bind the SelectedValue of a ComboBox
I trying to determine display vertical size of my Blackberry Storm 2. I know,
I'm trying to determine if the current system is 32 or 64 bit. And
I am trying to determine if a phone is located in this polygon using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.