Using IEnumerable without foreach loop

后端 未结 7 1029

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<string> BeautifulLadies()
    {
        yield return "Scarlett";
        yield return "Alexandra";
        yield return "Alisson";
    }
    
    // emulate next() in python
    public static T Next<T>(IEnumerator<T> iterator)
    {
        iterator.MoveNext();
        return iterator.Current;
    }
    
    0 讨论(0)
  • 2020-11-30 03:39

    Although the accepted answers are correct, notice that IEnumerator.Current is undefined before the first call to MoveNext().

    If you are iterating over a secondary array, you'll want something like:

    IEnumerable<Foo> Foo() { ... }
    
    int AssignValues(List<TakesFoo> data) {
      var count = 0;
      var foo = Foo().GetEnumerator();
    
      // Step into the first element of the array;
      // doing this does not discard a value from the IEnumerator
      if (foo.MoveNext()) { 
        foreach (var x in data) {
          x.SetFoo(foo.Current);
          count += 1;
          if (!foo.MoveNext()) { 
            break;
          }
       }
    
       // Return count of assigned values
       return count;
    }
    
    0 讨论(0)
  • 2020-11-30 03:42

    using for loop:

    for (var enumerator = getInt().GetEnumerator(); enumerator.MoveNext(); )
    {
        Console.WriteLine(enumerator.Current);
    }
    
    0 讨论(0)
  • 2020-11-30 03:44

    How about this?

    IEnumerator<int> iter = obj.getInt();
    using(iter) {
        while(iter.MoveNext()) {
            DoSomethingWith(iter.Current)
        }
    }
    
    0 讨论(0)
  • 2020-11-30 03:44

    It's important to mention that the duty of the foreach loop is to dispose the enumerator if the instance implements IDisposable. In other words, foreach should be replaced with something like:

    var enumerator = enumerable.GetEnumerator();
    
    try
    {
        while (enumerator.MoveNext())
        {
            var item = enumerator.Current;
            // do stuff
        }
    }
    finally
    {
        var disposable = enumerator as IDisposable;
        if (disposable != null)
        {
            disposable.Dispose();
        }
    }
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
提交回复
热议问题