.MoveNext() Method

135
Views
1
Answers

How to use .MoveNext() method with collections ?

edit | flag
Mad Maddy
Asked on: Feb 02, 2012 at 3:52AM

1 Answers

119
5
119

.MoveNext() is used with Enumerators.This method advances the enumerator to the next element of the collection.
For example

Dictionary dict = new Dictionary();
            dict
.Add("1", "January");
            dict
.Add("2", "February");
            dict
.Add("3", "March");
            dict
.Add("4", "April");
            dict
.Add("5", "May");
            dict
.Add("6", "June");
            dict
.Add("7", "July");

           
IDictionaryEnumerator dictenum = dict.GetEnumerator();
           
while (dictenum.MoveNext())
           
{
               
Console.WriteLine("Key: {0}  Value: {1}",
                                    dictenum
.Key, dictenum.Value);
           
}

Output:

           Key: 1 Value: January
           
Key: 2 Value: February
           
Key: 3 Value: March
           
Key: 4 Value: April
           
Key: 5 Value: May
           
Key: 6 Value: June
           
Key: 7 Value: July

.MoveNext() will return true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.

edit | flag
Gokul A
Answered on: Feb 02, 2012 at 3:59AM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags