为泛型列表创建迭代器

在本示例中,泛型Stack<T>实现泛型接口 IEnumerator<T>。声明了一个类型 T 的数组,并使用 Push 方法给数组赋值。在 GetEnumerator 方法中,使用 yield return 语句返回数组的值。

还实现非泛型 GetEnumerator,因为 IEnumerable<T> 继承自 IEnumerable。此示例显示了典型的实现,在该实现中,非泛型方法直接将调用转给泛型方法。

using  System.Collections;
using  System.Collections.Generic;

namespace  GenericIteratorExample
{
    
public class Stack<T> : IEnumerable<T>
    
{
        
private T[] values = new T[100];
        
private int top = 0;

        
public void Push(T t) { values[top++= t; }
        
public T Pop() return values[--top]; }

        
// These make Stack<T> implement IEnumerable<T> allowing
        
// a stack to be used in a foreach statement.
        public IEnumerator<T> GetEnumerator()
        
{
            
for (int i = top; -->= 0; )
            
{
                yield 
return values[i];
            }

        }


        IEnumerator IEnumerable.GetEnumerator()
        
{
            
return GetEnumerator();
        }


        
// Iterate from top to bottom.
        public IEnumerable<T> TopToBottom
        
{
            
get
            
{
                
// Since we implement IEnumerable<T>
                
// and the default iteration is top to bottom,
                
// just return the object.
                return this;
            }

        }


        
// Iterate from bottom to top.
        public IEnumerable<T> BottomToTop
        
{
            
get
            
{
                
for (int i = 0; i < top; i++)
                
{
                    yield 
return values[i];
                }

            }

        }


        
//A parameterized iterator that return n items from the top
        public IEnumerable<T> TopN(int n)
        
{
            
// in this example we return less than N if necessary 
            int j = n >= top ? 0 : top - n;

            
for (int i = top; -->= j; )
            
{
                yield 
return values[i];
            }

        }

    }


    
//This code uses a stack and the TopToBottom and BottomToTop properties 
    
//to enumerate the elements of the stack.
    class Test
    
{
        
static void Main()
        
{
            Stack
<int> s = new Stack<int>();
            
for (int i = 0; i < 10; i++)
            
{
                s.Push(i);
            }


            
// Prints: 9 8 7 6 5 4 3 2 1 0
            
// Foreach legal since s implements IEnumerable<int>
            foreach (int n in s)
            
{
                System.Console.Write(
"{0} ", n);
            }

            System.Console.WriteLine();

            
// Prints: 9 8 7 6 5 4 3 2 1 0
            
// Foreach legal since s.TopToBottom returns IEnumerable<int>
            foreach (int n in s.TopToBottom)
            
{
                System.Console.Write(
"{0} ", n);
            }

            System.Console.WriteLine();

            
// Prints: 0 1 2 3 4 5 6 7 8 9
            
// Foreach legal since s.BottomToTop returns IEnumerable<int>
            foreach (int n in s.BottomToTop)
            
{
                System.Console.Write(
"{0} ", n);
            }

            System.Console.WriteLine();

            
// Prints: 9 8 7 6 5 4 3
            
// Foreach legal since s.TopN returns IEnumerable<int>
            foreach (int n in s.TopN(7))
            
{
                System.Console.Write(
"{0} ", n);
            }

            System.Console.WriteLine();
        }

    }

}

输出

9 8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3

转载于:https://www.cnblogs.com/xiaxia/articles/910293.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值