Using IEnumerable without foreach loop

后端 未结 7 1045

I\'ve gotta be missing something simple here.

Take the following code:

public IEnumerable getInt(){
  for(int i = 0; i < 10; i++){
   y         


        
7条回答
  •  臣服心动
    2020-11-30 03:47

    You can get a reference to the Enumerator, using the GetEnumerator method, then you can use the MoveNext() method to move on, and use the Current property to access your elements:

    var enumerator = getInt().GetEnumerator();
    while(enumerator.MoveNext())
    {
        int n = enumerator.Current;
        Console.WriteLine(n);
    }
    

提交回复
热议问题