【问题标题】:Can C# interfaces implement lists or arrays? If so, how?C# 接口可以实现列表或数组吗?如果是这样,怎么做?
【发布时间】:2021-03-20 05:50:04
【问题描述】:

我正在将一堆 typescript 类和接口翻译成 C#(对于那些想知道为什么的 javascript 互操作)

我不确定如何从打字稿翻译的一个例子是:

interface CellArray extends Array<Cell> {
    addClass(className: string): CellArray;
    removeClass(className: string): CellArray;
    html(html: string): CellArray;
    invalidate(): CellArray;
}

如果我没看错的话,那个接口是在扩展单元格类型数组的类型...同时也有一些返回方法。

有没有办法把它翻译成 C#?

谢谢!

【问题讨论】:

  • 您可能是指扩展其他接口的接口——而不是实现.....
  • 你显然不能在 C# 中使用typescriptlang.org/docs/handbook/…,所以interface CellArray : IList&lt;Cell&gt; 可能是你能得到的最好的......解释你打算如何使用结果可能会帮助某人提出这样的方法。
  • Alexei,接口 CellArray : IList 是我需要的。谢谢!

标签: c# typescript class interface .d.ts


【解决方案1】:

C# 接口不能直接从 List 继承(因为它们是类) - 但它们可以从任何接口继承,例如 IListIEnumerable

Array 是一个继承自IListIEnumerable 的类。所以你可以继承IList&lt;T&gt;

    public interface Test<T> : IList<T>
    {
        // code here
    }

或者你可以从IEnumerable继承:

    public interface Test<T> : IEnumerable<T>
    {
        // code here
    }

【讨论】:

    猜你喜欢
    • 2012-04-08
    • 1970-01-01
    • 2021-06-30
    • 2017-11-14
    • 1970-01-01
    • 2020-01-21
    • 2011-04-22
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多