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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:51:54+00:00 2026-05-26T22:51:54+00:00

I have a class called GenericPermutations that is both enumerable and an enumerator. Its

  • 0

I have a class called GenericPermutations that is both enumerable and an enumerator. Its job is to take an ordered list of objects and iterate through each permutation of them in order.

Example, an integer implemenation of this class could iterate through the following:

GenericPermutations<int> p = new GenericPermutations<int>({ 1, 2, 3 });
p.nextPermutation(); // 123
p.nextPermutation(); // 132
p.nextPermutation(); // 213
// etc.

So its enumerable in the sense that it contains a ‘list’ of things you can enumerate over. It’s also an enumerator, because its job involves finding the next permutation.

THE ISSUE: I am currently trying to integrate IEnumerator and IEnumerable with this class, and it seems to me like it should be both (rather than using a sub class as the IEnumerable). Thus far I have avoided the issue with trying to get two enumerators from it by passing a new GenericPermutation object in the GetEnumerator method.

Is this a bad idea? Anything else I should consider?

  • 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-26T22:51:55+00:00Added an answer on May 26, 2026 at 10:51 pm

    Reduce your confusion (?) by using the generic versions of IEnumerable and IEnumerator.

    A permutation enumerable is IEnumerable<IEnumerable<T>>. So you might have something like

    IEnumerable<IEnumerable<T>> GetPermutations(IEnumerable<T> sequence)
    {
        return new Permuter<T>(sequence);
    }
    

    and

    public class Permuter<T> : IEnumerable<IEnumerable<T>> { ... }
    

    Furthermore, I’ve seen more than one case where a single type implemented both IEnumerable<T> and IEnumerator<T>; its GetEnumerator method was simply return this;.

    I think such a type would need to be a struct, though, because if it were a class you’d have all sorts of problems if you called GetEnumerator() a second time before the first enumeration was completed.

    EDIT: Consuming the permuter

    var permuter = GetPermutations(sequence);
    foreach (var permutation in permuter)
    {
        foreach (var item in permutation)
            Console.Write(item + "; ");
        Console.WriteLine();
    }
    

    Assuming the input sequence is { 1, 2, 3 }, the output is

    1; 2; 3; 
    1; 3; 2; 
    2; 1; 3; 
    2; 3; 1; 
    3; 1; 2; 
    3; 2; 1; 
    

    EDIT:

    Here’s a super-inefficient implementation to illustrate the suggestion:

    public class Permuter<T> : IEnumerable<IEnumerable<T>>
    {
        private readonly IEnumerable<T> _sequence;
    
        public Permuter(IEnumerable<T> sequence)
        {
            _sequence = sequence;
        }
    
        public IEnumerator<IEnumerable<T>> GetEnumerator()
        {
            foreach(var item in _sequence)
            {
                var remaining = _sequence.Except(Enumerable.Repeat(item, 1));
                foreach (var permutation in new Permuter<T>(remaining))
                    yield return Enumerable.Repeat(item, 1).Concat(permutation);
            }
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Foo that defines a list of objects of type
I have a container class (called Atom) that I want to store objects of
I have a class called people that has its own name, id etc... I
I have a class called DatabaseHelper that wraps a DbConnection. What's the proper way
I have a class called UserInfo that contains details about a given user. There
I have a class called Question that has a property called Type. Based on
I have one class called Person that basically looks like: public class Person {
I have a class called forest and a property called fixedPositions that stores 100
I have a class called Image and a class that extends it called ImageSheet
I have Class called Person containing to properties, Father and List of Children. I

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.