[Solved] I got movenext() exception [closed]


To use MoveNext() you need to keep hold of the iterator and just… call MoveNext(), as shown below.

The most common reason that MoveNext() would throw an exception is that the collection was modified – you added/removed/replaced an item. That isn’t usually allowed, so you’d have to construct your code to not do that. Perhaps by tracking planned changes when you iterate, then applying them after you’ve iterated.


var iter = users.GetEnumerator();
using(iter as IDisposable)
{
    while(iter.MoveNext())
    {
        var user = (MemberUser)iter.Current;
        // ...
    }
}

which is the same as:

foreach(MemberUser user in users) {
    // ...
}

Note that ideally you’d have IEnumerator<MemberUser>, which would simplify this greatly:

using(var iter = users.GetEnumerator())
{
    while(iter.MoveNext())
    {
        var user = iter.Current;
        // ...
    }
}

which is the same as:

foreach(var user in users) {
    // ...
}

solved I got movenext() exception [closed]