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.