C# 介面型別


介面定義屬性、方法和事件,它們是介面的成員。介面只包含成員的宣告。

C# 中的一些介面型別包括:

  • IEnumerable − 所有泛型集合的基本介面。

  • IList − 陣列和列表型別實現的一個泛型介面。

  • IDictionary − 一個字典集合。

IEnumerable 是一個介面,定義了一個返回 IEnumerator 介面的單一方法 GetEnumerator。

這適用於對實現 IEnumerable 的集合的只讀訪問,可以用在 foreach 語句中。

以下程式碼演示了 IEnumerable 介面的實現:

示例

class Demo : IEnumerable, IEnumerator {
   // IEnumerable method GetEnumerator()
   IEnumerator IEnumerable.GetEnumerator() {
      throw new NotImplementedException();
   }
   public object Current {
      get { throw new NotImplementedException(); }
   }
   // IEnumertor method
   public bool MoveNext() {
      throw new NotImplementedException();
   }
   // IEnumertor method
   public void Reset() {
      throw new NotImplementedException();
   }
}

以上您可以看到 IEnumerator 的兩種方法。

// IEnumerator method
public bool MoveNext() {
   throw new NotImplementedException();
}
// IEnumertor method
public void Reset() {
   throw new NotImplementedException();
}

更新於: 2020 年 6 月 23 日

4K+ 檢視次數

開啟你的事業

完成課程獲得認證

馬上開始
廣告
© . All rights reserved.