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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:08:11+00:00 2026-05-16T12:08:11+00:00

I am having a difficult time with a seemingly easy and embarrassing problem. All

  • 0

I am having a difficult time with a seemingly easy and embarrassing problem. All I want is the next element in an IEnumberable without using Skip(1).Take(1).Single(). This example illustrates the basic problem.

private char _nextChar;
private IEnumerable<char> getAlphabet()
{
    yield return 'A';
    yield return 'B';
    yield return 'C';
}
public void sortAlphabet()
{
     foreach (char alpha in getAlphabet())
     {
         switch (alpha)
         {
             case 'A':  //When A pops up, I want to get the next element, ie 'B'
                 _nextChar = getAlphabet().Skip(1).Take(1).Single();
                 break;
             case 'B': //When B pops up, I want 'C' etc
                 _nextChar = getAlphabet().Skip(1).Take(1).Single();
                 break;
         }
     }
}

Other than being ugly, this example works. But let’s say that the IEnumerable contained 2 million elements, then the LINQ statement makes the program execute unbearably slow. What I want is simple. I just want the next element in an IEnumberable<>. All my problems would be solved if there was a function like:

_nextChar = getAlphabet().moveNext() //or getNext()

It is much preferred if the solution keeps the same structure/layout/functionality of the example however, I am flexible. My program is a file parser, and among the 2 million lines of text are some keys like “money=324” where “money” and “324” are neighbor elements in the IEnumberable and when the parser comes across “money” I want “324”. (who doesn’t? 😀 Sorry for bad pun.)

  • 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-16T12:08:11+00:00Added an answer on May 16, 2026 at 12:08 pm

    All my problems would be solved if
    there was a function like:

    _nextChar = getAlphabet().moveNext() //or getNext()

    There is a function exactly like that. It just belongs to IEnumerator<T>, not IEnumerable<T>!

    private char _nextChar;
    private IEnumerable<char> getAlphabet()
    {
        yield return 'A';
        yield return 'B';
        yield return 'C';
    }
    
    public void sortAlphabet()
    {
        using (var enumerator = getAlphabet().GetEnumerator())
        {
            while (enumerator.MoveNext())
            {
                char alpha = enumerator.Current;
                switch (alpha)
                {
                    case 'A':
                        if (enumerator.MoveNext())
                        {
                            _nextChar = enumerator.Currrent;
                        }
                        else
                        {
                            // You decide what to do in this case.
                        }
                        break;
                    case 'B':
                        // etc.
                        break;
                }
            }
        }
    }
    

    Here’s a question for you, though. Is it necessary that this code use an IEnumerable<char>, rather than an IList<char>? I ask because, as if this weren’t obvious, the code would be much simpler if you had random access to the items returned by getAlphabet by index (and if someone is tempted to point out that you can do this with ElementAt, please, just get that idea out of your head right now).

    I mean, consider what the code would look like in this case:

    private char _nextChar;
    private IList<char> getAlphabet()
    {
        return Array.AsReadOnly(new[] { 'A', 'B', 'C' });
    }
    
    public void sortAlphabet()
    {
        IList<char> alphabet = getAlphabet();
        for (int i = 0; i < alphabet.Count - 1; ++i)
        {
            char alpha = alphabet[i];
            switch (alpha)
            {
                case 'A':
                    _nextChar = alphabet[i + 1];
                    break;
                case 'B':
                    // etc.
                    break;
            }
        }
    }
    

    Isn’t that much easier?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 534k
  • Answers 534k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One solution would be to use a LINQ query which… May 17, 2026 at 12:40 am
  • Editorial Team
    Editorial Team added an answer Keep an eye on the following tags #java #openjdk and… May 17, 2026 at 12:40 am
  • Editorial Team
    Editorial Team added an answer Yes, you can absolutely install directly from your own website.… May 17, 2026 at 12:40 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am having a difficult time with management asking for estimates on programming tasks
I'm having a difficult time understanding where actual Leaks are happening and where they
I am having a difficult time deciding how to handle a business requirement in
I am having a difficult time forming a conditional INSERT I have x_table with
I am having a difficult time reading an XML file with Cdata inside. in
I am having a difficult time trying to set up Cruise (not cruise control)
I am having a difficult time wrapping my head around the jQuery eq .
I'm having a difficult time figuring out how to add a .jar/library to a
I'm having a difficult time locating an HTML parser that works with JRuby. I've
I'm having a difficult time retaining the programming skills I'm actively trying to learn

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.