C# 接口类型

来源:这里教程网 时间:2026-02-21 16:32:04 作者:

c# 接口类型

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

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

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

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

IDictionary - 字典集合。

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

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

这适用于对集合的只读访问。 p>

下面展示了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();
}

相关推荐