当前位置:首页 > 网络编程 > 软件语言 > .NET > C# 程序员参考--集合类教程

C# 程序员参考--集合类教程

点击次数:25 次 发布日期:2008-11-06 08:04:49 作者:源代码网
源代码网推荐
广告载入中

本教程展示如何实现可与 foreach 语句一起使用的集合类。

教程

foreach 语句是循环访问数组元素的方便方法。如果集合类已实现 System.Collections.IEnumeratorSystem.Collections.IEnumerable 接口,它还可以枚举该集合的元素。

示例 1

下面的代码示例阐释如何编写可与 foreach 一起使用的集合类。该类是字符串标记化拆分器,类似于 C 运行时库函数 strtok

// tokens.cs

using System;

// The System.Collections namespace is made available:

using System.Collections;



// Declare the Tokens class:

public class Tokens : IEnumerable

{

   private string[] elements;



   Tokens(string source, char[] delimiters)

   {

      // Parse the string into tokens:

      elements = source.Split(delimiters);

   }



   // IEnumerable Interface Implementation:

   //   Declaration of the GetEnumerator() method 

   //   required by IEnumerable

   public IEnumerator GetEnumerator()

   {

      return new TokenEnumerator(this);

   }



   // Inner class implements IEnumerator interface:

   private class TokenEnumerator : IEnumerator

   {

      private int position = -1;

      private Tokens t;



      public TokenEnumerator(Tokens t)

      {

         this.t = t;

      }



      // Declare the MoveNext method required by IEnumerator:

      public bool MoveNext()

      {

         if (position < t.elements.Length - 1)

         {

            position  ;

            return true;

         }

         else

         {

            return false;

         }

      }



      // Declare the Reset method required by IEnumerator:

      public void Reset()

      {

         position = -1;

      }



      // Declare the Current property required by IEnumerator:

      public object Current

      {

         get

         {

            return t.elements[position];

         }

      }

   }



   // Test Tokens, TokenEnumerator



   static void Main()

   {

      // Testing Tokens by breaking the string into tokens:

      Tokens f = new Tokens("This is a well-done program.", 

         new char[] {" ","-"});

      foreach (string item in f)

      {

         Console.WriteLine(item);

      }

   }

} 

输出

This

is

a

well

done

program. 

代码讨论

在前面的示例中,下列代码用于 Tokens 化,方法是将“This is a well-done program.”拆分为标记(使用“”和“-”作为分隔符),并用 foreach 语句枚举这些标记:

Tokens f = new Tokens("This is a well-done program.", 

   new char[] {" ","-"});

foreach (string item in f)

{

   Console.WriteLine(item);

} 软件开发网 www.mscto.com 

请注意,Tokens 在内部使用一个数组,自行实现 IEnumeratorIEnumerable。该代码示例本可以利用数组本身的枚举方法,但那会使本示例的目的失效。

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

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

public class Tokens  // no longer inherits from IEnumerable

public TokenEnumerator GetEnumerator()  // doesn"t return an IEnumerator

public class TokenEnumerator  // no longer inherits from IEnumerator

public string Current  // type-safe: returns string, not object  

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

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

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

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

示例 2

本示例功能与“示例 1”等效,但它在维持与其他语言互操作性的同时还提供 C# 中的类型安全。

// tokens2.cs

using System;

using System.Collections;



public class Tokens: IEnumerable

{

   private string[] elements;



   Tokens(string source, char[] delimiters)

   {

      elements = source.Split(delimiters);

   }



   // IEnumerable Interface Implementation:



   public TokenEnumerator GetEnumerator() // non-IEnumerable version

   {

      return new TokenEnumerator(this);

   }



   IEnumerator IEnumerable.GetEnumerator() // IEnumerable version

   {

      return (IEnumerator) new TokenEnumerator(this);

   }



   // Inner class implements IEnumerator interface:



   public class TokenEnumerator: IEnumerator

   {

      private int position = -1;

      private Tokens t;



      public TokenEnumerator(Tokens t)

      {

         this.t = t;

      }



      public bool MoveNext()

      {

         if (position < t.elements.Length - 1)

         {

            position  ;

            return true;

         }

         else

         {

            return false;

         }

      }



      public void Reset()

      {

         position = -1;

      }



      public string Current // non-IEnumerator version: type-safe

      {

         get

         {

            return t.elements[position];

         }

      }



      object IEnumerator.Current // IEnumerator version: returns object

      {

         get

         {

            return t.elements[position];

         }

      }

   }



   // Test Tokens, TokenEnumerator



   static void Main()

   {

      Tokens f = new Tokens("This is a well-done program.", 

         new char [] {" ","-"});

      foreach (string item in f) // try changing string to int

      {

         Console.WriteLine(item);

      }

   }

} 软件开发网 www.mscto.com 

软件开发网 www.mscto.com


源代码网推荐

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华