• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • C# 程序員參考--集合類教程

    發表于:2007-07-14來源:作者:點擊數: 標簽:
    C# 程序員 參考--集合類教程: 本教程展示如何實現可與 foreach 語句一起使用的集合類。 教程 foreach 語句是循環訪問數組元素的方便方法。如果集合類已實現 System.Collections.IEnumerator 和 System.Collections.IEnumerable 接口,它還可以枚舉該集合的
    C# 程序員參考--集合類教程:

    本教程展示如何實現可與 foreach 語句一起使用的集合類。

    教程

    foreach 語句是循環訪問數組元素的方便方法。如果集合類已實現 System.Collections.IEnumeratorSystem.Collections.IEnumerable 接口,它還可以枚舉該集合的元素。

    示例 1

    下面的代碼示例闡釋如何編寫可與 foreach 一起使用的集合類。該類是字符串標記化拆分器,類似于 C 運行時庫函數 strtok。

    // tokens.csusing 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);      }   }}

    輸出

    Thisisawelldoneprogram.

    代碼討論

    在前面的示例中,下列代碼用于 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);}

    請注意,Tokens 在內部使用一個數組,自行實現 IEnumeratorIEnumerable。該代碼示例本可以利用數組本身的枚舉方法,但那會使本示例的目的失效。

    在 C# 中,集合類并非必須嚴格從 IEnumerable IEnumerator 繼承才能與 foreach 兼容;只要類有所需的 GetEnumerator、MoveNext、ResetCurrent 成員,便可以與 foreach 一起使用。省略接口的好處為,使您可以將 Current 的返回類型定義得比 object 更明確,從而提供了類型安全。

    例如,從上面的示例代碼開始,更改以下幾行:

    public class Tokens  // no longer inherits from IEnumerablepublic TokenEnumerator GetEnumerator()  // doesn't return an IEnumeratorpublic class TokenEnumerator  // no longer inherits from IEnumeratorpublic 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.csusing 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);      }   }}

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>