Ken H

Ken H

  • NA
  • 646
  • 360.6k

How to use ‘foreach’ in IEnumerator?

Jan 2 2015 3:58 AM
Hello friend,
    
Questions are as follows: 
 
private void button1_Click(object sender, EventArgs e){
List<int> list = new List<int>();
for (int i = 1; i <= 10; i++) list.Add(i);
IEnumerable<int> enumer = list;
foreach (int item in enumer)
{
  textBox1.Text += item.ToString() + "|";
}
IEnumerator<int> enumer2 = list.GetEnumerator();
while (enumer2.MoveNext())
{
  textBox2.Text += enumer2.Current.ToString() + "|";
}
IEnumerator<int> enumer3 = list.GetEnumerator();
foreach (int item in enumer3)     // Try use foreach,but compilation errors in here.
{
  textBox3.Text += item.ToString() + "|";
}
}
 
Thanks.
 

Answers (7)