c# 接口(interface)实现 - 为什么不构建?

标签 c# arrays interface ienumerable implementation

很抱歉,如果之前有人问过这个问题,但用谷歌搜索几乎是不可能的。我认为 int 数组实现了 IEnumerable,因此 Thing 应该能够实现 IThing。怎么没有?

public interface IThing
{
    IEnumerable<int> Collection { get; }
}

public class Thing : IThing
{
    public int[] Collection { get; set; }
}

注意

public class Thing : IThing
{
    public int[] Array { get; set; }
    public IEnumerable<int> Collection
    {
         get
         {
              return this.Array;
         }
    }
}

很好。

最佳答案

对于要实现的接口(interface),方法签名和返回类型必须相同,因此 int[] 可转换为 IEnumerable 的事实恐怕没有什么区别。

关于c# 接口(interface)实现 - 为什么不构建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8246886/

相关文章:

c# - 我想从 MySQL 数据库中检索去年的记录,但它正在从数据库中检索所有数据

c# - 嵌入或引用项目

c++ - 初始化结构数组 C++ 时出现问题

c# - 编辑接口(interface)时叫什么?

.net - 如何在 IronPython 中实现接口(interface)?

c# - 如何在我的单元测试项目中使用没有 .NET 5 的 RavenDB.RavenTestDriver 5+?

c# - 缺少 WPF 程序集引用 - 项目仍在 build 中

sql - 更新 PostgreSQL 数组中的值

arrays - Ruby - 数组交集(有重复项)

node.js - 如何扩展 Error 类?