C# 接口类型

csharpprogrammingserver side programming更新于 2024/9/12 7:55:00

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

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

  • IEnumerable − 所有泛型集合的基本接口。

  • IList − 由数组和列表类型实现的泛型接口。

  • IDictionary −字典集合。

IEnumerable 是一个接口,它定义了一个返回 IEnumerator 接口的 GetEnumerator 方法。

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

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

示例

class Demo : IEnumerable, IEnumerator {
   // IEnumerable 方法 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 方法
public bool MoveNext() {
   throw new NotImplementedException();
}
// IEnumertor 方法
public void Reset() {
   throw new NotImplementedException();
}

相关文章