C#接口类型

接口定义属性,方法和事件,它们是接口的成员。接口仅包含成员的声明。

C#中的某些接口类型包括。

  • IEnumerable-所有通用集合的基本接口。

  • IList-由数组和列表类型实现的通用接口。

  • IDictionary-词典集合。

IEnumerable是定义单个方法GetEnumerator的接口,该方法返回IEnumerator接口。

这适用于对实现IEnumerable可以与foreach语句一起使用的集合的只读访问。

下面显示了IEnumerable接口的实现。

示例

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

在上面可以看到IEnumerator的两种方法。

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