Why can't iterator methods take either 'ref' or 'out' parameters?

前端 未结 5 1316
鱼传尺愫
鱼传尺愫 2021-01-31 08:36

I tried this earlier today:

public interface IFoo
{
    IEnumerable GetItems_A( ref int somethingElse );
    IEnumerable GetItems_B( ref in         


        
5条回答
  •  走了就别回头了
    2021-01-31 08:40

    I've gotten around this problem using functions, when the value that I need to return is derived from the iterated items:

    // One of the problems with Enumerable.Count() is
    // that it is a 'terminator', meaning that it will
    // execute the expression it is given, and discard
    // the resulting sequence. To count the number of
    // items in a sequence without discarding it, we 
    // can use this variant that takes an Action
    // (or Action), invokes it and passes it the
    // number of items that were yielded.
    //
    // Example: This example allows us to find out
    //          how many items were in the original
    //          source sequence 'items', as well as
    //          the number of items consumed by the
    //          call to Sum(), without causing any 
    //          LINQ expressions involved to execute
    //          multiple times.
    // 
    //   int start = 0;    // the number of items from the original source
    //   int finished = 0; // the number of items in the resulting sequence
    //
    //   IEnumerable> items = // assumed to be an iterator
    //
    //   var result = items.Count( i => start = i )
    //                   .Where( p => p.Key = "Banana" )
    //                      .Select( p => p.Value )
    //                         .Count( i => finished = i )
    //                            .Sum();
    //
    //   // by getting the count of items operated 
    //   // on by Sum(), we can calculate an average:
    // 
    //   double average = result / (double) finished; 
    //
    //   Console.WriteLine( "started with {0} items", start );
    //   Console.WriteLine( "finished with {0} items", finished );
    //
    
    public static IEnumerable Count( 
        this IEnumerable source, 
        Action receiver )
    {
      int i = 0;
      foreach( T item in source )
      {
        yield return item;
        ++i ;
      }
      receiver( i );
    }
    
    public static IEnumerable Count( 
        this IEnumerable source, 
        Action receiver )
    {
      long i = 0;
      foreach( T item in source )
      {
        yield return item;
        ++i ;
      }
      receiver( i );
    }
    

提交回复
热议问题