Get to next element in List

20,033

Solution 1

http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx

The link above this line of text has what will work to solve my issue.

Thanks all for the responses and help! Upvoted those who tried to help and had something to offer

Solution 2

I hate to sound like a dinosaur, but since you're implementing with a List anyway, why not iterate over it with for instead of foreach? Integers are really useful for comparisons like i == j + 1

Solution 3

Looks like you really are re-inventing an enumerator:

public IEnumerator<Employee> GetEmployees()
{
    foreach (Employee employee in ListOfEmployees)
    {
        //custom processing here
        yield return employee;
    }
}

Usage:

var myEnumerator = foo.GetEmployees();
while(myEnumerator.MoveNext())
{
    var someEmployee = myEnumerator.Current;
    //do something
}

Just as an update here is the full class implementation so you can verify it compiles and works..

public class Foo
{
    List<Employee> ListOfEmployees = new List<Employee>();

    public Foo()
    {
        ListOfEmployees.Add(new Employee());
    }

    public IEnumerator<Employee> GetEmployees()
    {
        foreach (Employee employee in ListOfEmployees)
            yield return employee;
    }
}

Solution 4

(As an academic exercise, the other answers are probably more appropriate here: )

You could create an extension method like so:

public static IEnumerable<Tuple<T, T>> ToPairs<T>(this IEnumerable<T> enumerable)
    {
        using (var enumerator = enumerable.GetEnumerator())
        {
            if (enumerator.MoveNext())
            {
                var previous = enumerator.Current;
                while (enumerator.MoveNext())
                {
                    var current = enumerator.Current;
                    yield return new Tuple<T, T>(previous, current);
                    previous = current;
                }
            }
        }
    }

To return you a tuple containing pairs of elements.

Which would be used like:

foreach (var pair in ListOfEmployees.ToPairs())
{
   Employee prevEmployee = pair.Item1;
   Employee currEmployeeObj = pair.Item2;

}
Share:
20,033
BigBug
Author by

BigBug

Updated on July 09, 2022

Comments

  • BigBug
    BigBug almost 2 years

    If i have a list of objects and i want to move to the next node with each function call (ie create a "GetNextNode" how would i go about doing this? Right now i have one method which will get the first node of my List and set the currentObj to it and return it (leaving previous node still at null) a flag indicates that we're not dealing with the first node in the list anymore. then i move forward and i want to iterate through the list (using foreach i guess?) to one node past my currentObj. Here is my code:

     List<Employee> ListOfEmployees = new List<Employee>();
     Employee currEmployeeObj = null;
     Employee prevEmployeeObj = null;
    
     foreach (Employee employee in ListOfEmployees)
            {
               //how do i keep track of the previous and current employee in here?
            }
    
            return (currEmployeeObj); 
        }