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 202757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:19:04+00:00 2026-05-11T17:19:04+00:00

So for a type like: CoolCollection<T> you could have: foreach (T item in coolCollection)

  • 0

So for a type like:

CoolCollection<T>

you could have:

foreach (T item in coolCollection)
{
    ...
}

foreach (CoolNode node in coolCollection)
{
    ...
}

If this isn’t possible, maybe like foreach2, or some other way to iterate. Often times, I would really like more than 1 way of iterating on a type.

EDIT: Sorry if it wasn’t clear. Basically CoolNode is a node that makes CoolCollection. CoolNode has a property called value to return T, but I need another iterator to return only CoolNodes.

EDIT2: I can’t do coolCollection.Something to iterate, because CoolNodes are connected via a property called Next, like a LinkedList. So I need to implement 2 iterators.

  • 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-11T17:19:05+00:00Added an answer on May 11, 2026 at 5:19 pm

    Just make CoolCollection<T> explicitly implement IEnumerable<CoolNode<T>> as well as IEnumerable<T>. (I’m guessing it’s really CoolNode<T>, but if not, just take the extra <T> out everywhere.)

    This will let you iterate in both manners, although you’ll need a cast.

    To do this, you’d need something like:

    class CoolCollection<T> : ICollection<T>, IEnumerable<CoolNode<T>>
    {
        IEnumerator<CoolNode<T>> IEnumerable<CoolNode<T>>.GetEnumerator()
        {
            ///...Do work here...
        }
    
        IEnumerator<T> GetEnumerator()
        {
            ///...Do work here...
        }
    }
    

    Using this would be like so:

    foreach (T item in coolCollection)
    {
        ...
    }
    
    
    foreach (CoolNode<T> node in (IEnumerable<CoolNode<T>>)coolCollection)
    {
        ...
    }
    

    The other option would be to expose a property for the “nodes”, so you could do:

    foreach(var nodes in coolCollection.Nodes)
    { ... }
    

    To implement this, you’d change things around a little bit. You’d need to make a private class that implemented the enumerator… something like:

    class CoolCollection<T> : ICollection<T>
    {
        private List<CoolNode<T>> nodes;
    
        IEnumerable<CoolNode<T>> Nodes
        {
            get 
            {
                 foreach(var node in this.nodes) { yield return node; }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a type like this: class Foo { public: int bar[3]; /*Possibly some
I have some string that have a type like this NSString *string1 = @/Users/mine/Library/Application
I have an enum type like this as an example: public Enum MyEnum {
Imagine I have a recursive algebraic data type like this (Haskell syntax): data Expr
I have a recursive data type like this: template<typename T> struct SomeType { std::map<T,
I have created a type like this: TypeBuilder tb = moduleBuilder.DefineType(myname, TypeAttributes.Class | TypeAttributes.Public,
I have a function with type like this: functionX :: [String] -> ([Integer] ->
In C# i have a collection for a type like this. public class scale
i have a query in access that is this: SELECT iif([Cup Type] like '*Cylinder*',count([Cup
When I have some function of type like f :: (Ord a) => a

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.