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

  • SEARCH
  • Home
  • 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 6148221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:14:34+00:00 2026-05-23T19:14:34+00:00

I am trying to write a generic comparison routine. However, some of the items

  • 0

I am trying to write a generic comparison routine. However, some of the items in my class are in Collections and I need to enumerate to compare.

Is there an easy way, without a try/catch block to determine is a variable supports GetEnumerator()

  • 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-05-23T19:14:35+00:00Added an answer on May 23, 2026 at 7:14 pm

    The answers suggesting you check to see if the object is an IEnumerable are reasonably spot on. It’s going to be the case the overwhelming majority of the time that your collection or object will implement that interface if it supports enumeration. But it is not required.

    For something to be enumerated in a foreach, it really only needs to expose a GetEnumerator() method that returns an object with suitable MoveNext() and Current implementations. Consider something like:

    class CustomCollection
    {
        public CustomEnumerator GetEnumerator()
        {
            return new CustomEnumerator();
        }
    }
    
    class CustomEnumerator
    {
        public bool MoveNext()
        {
            return (++this.Current <= 10);
        }
    
        public int Current { get; private set; }
    }
    

    You can put new CustomCollection() into a loop and get the values 1..10.

    By all means, check for the interface first. If that’s all you want to support, fine. If you want to go that extra mile, you’d need to perform steps like the compiler would, as in check for the appropriate methods and return types. Here’s a thoroughly untested draft of an implementation.

    bool IsEnumerable(object obj)
    {
        if (obj is IEnumerable)
            return true;
    
        var info = obj.GetType().GetMethod("GetEnumerator");
        if (info != null)
        {
            if (info.ReturnType != null)
            {
                var moveNextMethod = info.ReturnType.GetMethod("MoveNext");
                if (moveNextMethod != null && moveNextMethod.ReturnType == typeof(bool))
                {
                    var currentProperty = info.ReturnType.GetProperty("Current");
                    if (currentProperty != null)
                        return true;
                }
            }
        }
    
        return false;
    }
    

    Some quick tests in LinqPad, but by no means exhaustive…

    IsEnumerable(new CustomCollection()).Dump(); // true
    IsEnumerable(1).Dump(); // false 
    IsEnumerable(new List<int>()).Dump(); // true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write some generic structures. Essentially, what I need for my purpose
I'm trying to write a generic class which contains a generic TObjectList< T >
I am trying to write a generic module to extend the World class. I
I am trying to write some generic LINQ queries for my entities, but am
I am trying to write a generic one-size-fits-most repository pattern template class for an
I am trying to write some generic linqtoSQL which performs operation on entities based
Im trying to write a generic base class that will allow sub classes to
I am trying to write a generic library in pure c , just some
I'm trying to write some documentation and can't think of a generic name for
I'm trying to write some generic http response handler functions that sometimes open UIAlertViews.

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.