C # equivalent nextElement () from Java

What is equivalent to 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? } 
+4
source share
3 answers

You should use the foreach :

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

If you really want to use a raw enumeration, then you 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 this.

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

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

As the other guys suggest, you probably prefer to use the foreach .

0
source

Source: https://habr.com/ru/post/1486395/


All Articles