Using IEnumerable without foreach loop

后端 未结 7 1028

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:30

    Sample,

    public static void SampleIteratorNext()
    {
        var beauty = BeautifulLadies().GetEnumerator();
    
        Console.WriteLine($"Friday with {Next(beauty)} ...");
        Console.WriteLine($"Saturday with {Next(beauty)} ...");
        Console.WriteLine($"Tusday with {Next(beauty)} ...");
    }
    
    public static IEnumerable BeautifulLadies()
    {
        yield return "Scarlett";
        yield return "Alexandra";
        yield return "Alisson";
    }
    
    // emulate next() in python
    public static T Next(IEnumerator iterator)
    {
        iterator.MoveNext();
        return iterator.Current;
    }
    

提交回复
热议问题