4

What is the equivalent of nextElement() from Java?

I have the following code:

IEnumerable<String> e = (IEnumerable<String>)request
                                        .Params;

while (e.Count() > 1)
{
 // 
 //String name = e.nextElement();
String name = e. // what method?
}

3 Answers 3

9

You should be using the foreach loop:

foreach (string name in request.Params)
{
    // Do something for each name
}

If you really want to use the raw enumerable, then you have call its GetEnumerator() method:

using (IEnumerator<string> enumerator = request.Params.GetEnumerator())
{
    while (enumerator.MoveNext())
    {
        string name = enumerator.Current;

        // Do something for each name
    }
}

However, the foreach syntax is much clearer. Use that.

2
var enumerator = e.GetEnumerator();
while  (enumerator.MoveNext()) {
    var name = enumerator.Current;
}
0

What you're looking for is the IEnumerator.MoveNext() method. From an IEnumerable, you must first call the GetEnumerator() method to get an IEnumerator. Note that the IEnumerator interface is IDisposable which means, you need to use it inside a using clause.

As other guys suggest, you'll probably prefere using a foreach loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.