集合类型的foreach循环浅析

 .net framework支持任何 Collections 类都可以生成自己的枚举数,该枚举数简化了对元素的循环访问。在c#中,可以使用foreach循环来访问集合中的对象。使用foreach是集合的主要目的,集合没有提供其他特性。

集合必须实现接口System.Collections.IEnumerable. IEnumerable 只定义了一个方法:

public interface IEnumerable
{

        IEnumeator GetEnumerator()

}

GetEnumerator 的目的是返回实现了IEnumerator 的枚举对象。在下面的例子中,枚举对象为 ProgramEnumerable。

interface IEnumerator

{

object Current{get;} //read only

bool MoveNext();

void Reset();

}

IEumerator 的工作方式:实现该接口的对象与一个集合相关联 (如 array, arraylist 等),这个对象在第一次初始化时,还没有指向集合的任何元素,必须先调用movenext, 移动枚举,才能使他指向集合中的第一个元素,接着用current属性获取该元素,current 属性返回一个对象引用,所以必须把他的数据类型转换为要在集合中查找的对象类型。 然后再调用movenext方法移动到集合的下一个元素上。直到集合中没有元素为止。即current返回了一个null.

调用reset方法来返回到集合的开头的前面的位置,相当于array 的index = -1 的位置。接着就需要调用movenext把index = 0;

foreach 循环为集合类型提供了一种不需要index的遍历元素的方式,foreach只负责遍历所有的元素,元素的顺序由集合设定。

所以foreach循环实际上是以上IEnumerator, IEnumerable接口的简化版。Array属于集合,但不再System.Collections namespace  下。

集合的current把枚举作为一个object返回,因为多个枚举可应用在同一个集合上。 

在 C# 中,集合类并非必须严格从 IEnumerableIEnumerator 继承才能与 foreach 兼容;只要类有所需的 GetEnumeratorMoveNextResetCurrent 成员,便可以与 foreach 一起使用。省略接口的好处为,使您可以将 Current 的返回类型定义得比 object 更明确,从而提供了类型安全。

例如,从上面的示例代码开始,更改以下几行:

// No longer inherits from IEnumerable:
public class Tokens  
// Doesn't return an IEnumerator:
public TokenEnumerator GetEnumerator()  
// No longer inherits from IEnumerator:
public class TokenEnumerator  
// Type-safe: returns string, not object:
public string Current  

现在,由于 Current 返回字符串,因此当 foreach 语句中使用了不兼容的类型时,编译器便能够检测到:

// Error: cannot convert string to int:
foreach (int item in f)  

省略 IEnumerableIEnumerator 的缺点是,集合类不再能够与其他公共语言运行库兼容语言的 foreach 语句或等效项交互操作。

您可以同时拥有两者的优点,即 C# 中的类型安全以及与其他公共语言运行库兼容语言的互操作性,方法是从 IEnumerableIEnumerator 继承并使用显式接口实现,如下面的示例所示。

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

namespace  IEnumerable1
{
    
/// <summary>
    
/// 实现IEnumerable 把它标记为一个集合
    
/// </summary>


    
public class Program : IEnumerable
    
{
        
string[] str;

        
public Program(string text, char[] space)
        
{
            str 
= text.Split(space);
        }


        
//实现IEnumerable接口
        public IEnumerator GetEnumerator()
        
{
            
//枚举的实例引用
            return new ProgramEnumerable(this);
        }

        
/// <summary>
        
/// 私有类, 不能由外部代码访问
        
/// 定义枚举
        
/// </summary>


        
private class ProgramEnumerable: IEnumerator
        
{
            
int position = -1;
            Program p;

            
public ProgramEnumerable(Program pp)
            
{
                p 
= pp;
            }

            
public bool MoveNext()
            
{
                
if (position < (p.str.Length - 1))
                
{
                    
++position;
                    
return true;
                }

                
else
                    
return false;
            }

            
public object Current
            
{
                
get
                
{
                    
if (position > -1 && position < p.str.Length)
                        
return p.str[position];
                    
else
                        
throw new InvalidOperationException("blablabla");
                }

            }

            
public void Reset()
            
{
                position 
= -1;
            }


        }


        
static void Main(string[] args)
        
{
            Program pp 
= new Program("I so AM-hungry"new char[] ' ''-' });
            
foreach (string str in pp)
            
{
                Console.WriteLine(
"{0}", str);
            }

        }

    }

}

 

 


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值