简体   繁体   English

如何在C#中检查哪个枚举器为空?

[英]How to check which enumerator is empty in C#?

I have two enumerators of List<MyObject>.Enumerator and I am using this code: 我有两个List<MyObject>.Enumerator枚举器,并且正在使用此代码:

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? 那么,检查哪个枚举数不再能够调用.MoveNext()一种优雅方法是什么?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM