Search code examples
c#listenumerate

How to check which enumerator is empty in C#?


I have two enumerators of List<MyObject>.Enumerator and I am using this code:

using(List<MyObject>.Enumerator A = someListA.GetEnumerator())
using(List<MyObject>.Enumerator B = someListB.GetEnumerator())
{
    while(A.MoveNext() && B.MoveNext())
    {
        if (!A.Current.Equals(B.Current))
        {
            ...      
        }
    }
}

It is possible that one list is empty before the other one in my code. So what would be an elegant way of checking which enumerator is not able to call .MoveNext() anymore?


Solution

  • bool stateA;
    bool stateB;
    while ( (stateA = A.MoveNext()) & (stateB = B.MoveNext()))
    {
         if (!A.Current.Equals(B.Current))
         {
                        ...      
         }
    }
    //A' and B' states are available here