C# GetEnumerator用法

原文地址:http://www.cnblogs.com/scottckt/archive/2011/05/16/2048243.html

 

GetEnumerator是返回实例的枚举数。换句话说就是返回集的中所有元素一个一个列出来。我们可以通过MoveNext()得到集合中的所有元素。

这里的名词稍微说明一下,

    枚举就:是一个一个的列举。

    枚举数:是循环访问其关联集合的对象。它提供一种方法帮助你遍历一个集合。

  

GetEnumerator用法请看下边代码中红色部分。 这里顺便作了测试。发现GetEnumerator要比Foreach稍微快一点。

 

复制代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;

using  System.Diagnostics;
using  System.Collections;

namespace  GetEnumeratorTest
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            
const   int  times  = 1000 ;
            Stopwatch watch 
=   new  Stopwatch();
            Hashtable hastable 
=   new  Hashtable();
            
for  ( int  i  =   0 ; i  <   10000 ; i ++ )
            {
                hastable.Add(i, i.ToString() 
+   " " );
            }
            
// 测试GetEnumerator
            watch.Start();
            
for  ( int  i  =   0 ; i  <  times; i ++ )
            {
                
IDictionaryEnumerator enumerator = hastable.GetEnumerator();
                
while  (enumerator.MoveNext())
                {
                    
string  key  =  enumerator.Key.ToString();
                    
string  value  =  enumerator.Value.ToString();
                }
            }
            watch.Stop();
            Console.WriteLine(
" Hashtable GetEnumerator耗时 "   +  watch.ElapsedMilliseconds);
            Console.WriteLine(
" --------------- " );

            watch.Reset();
            
// 测试ForEach
            watch.Start();
            
for  ( int  i  =   0 ; i  <  times; i ++ )
            {
                
foreach  ( object  item  in  hastable.Keys)
                {
                    
string  key  =  item.ToString();
                    
string  value  =  hastable[item].ToString();
                }
            }
            watch.Stop();
            Console.WriteLine(
" Hashtable ForEach耗时 "   +  watch.ElapsedMilliseconds);
            Console.WriteLine(
" --------------- " );

            watch.Reset();
            Dictionary
< int string >  dictionary  =   new  Dictionary < int string > ();
            
for  ( int  i  =   0 ; i  <   10000 ; i ++ )
            {
                dictionary.Add(i, i.ToString() 
+   " " );
            }
            watch.Start();
            
for  ( int  i  =   0 ; i  <  times; i ++ )
            {                
                
Dictionary<int,string>.Enumerator enumerator = dictionary.GetEnumerator();
                
while  (enumerator.MoveNext())
                {
                    
int  key  =  enumerator.Current.Key;
                    
string  value  =  enumerator.Current.Value;
                }
            }
            watch.Stop();
            Console.WriteLine(
" Dictionary GetEnumerator耗时 "   +  watch.ElapsedMilliseconds);
            Console.WriteLine(
" --------------- " );

            watch.Reset();
            
// 测试ForEach
            watch.Start();
            
for  ( int  i  =   0 ; i  <  times; i ++ )
            {
                
foreach  ( int  item  in  dictionary.Keys)
                {
                    
int  key  =  item;
                    
string  value  =  dictionary[item];
                }
            }
            watch.Stop();
            Console.WriteLine(
" Dictionary ForEach耗时 "   +  watch.ElapsedMilliseconds);
            Console.WriteLine(
" --------------- " );
            Console.ReadKey();
        }
    }
}
复制代码

 

 结果:

    Hashtable GetEnumerator耗时 1584
    Hashtable ForEach耗时 1884
    Dictionary GetEnumerator耗时 365
    Dictionary ForEach耗时 375

你可能感兴趣的:(C# GetEnumerator用法)