Can C# interfaces implement lists or arrays? If so, how?

108 views Asked by At

I am translating a bunch of typescript classes and interfaces to C# (for javascript interop for those wondering why)

One example that I am not sure how to translate from typescript is this:

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

If i'm reading that correctly, that interface is extending the type of an array of type cell... while also having some return methods.

Is there a way to translate this to C#?

Thanks!

1

There are 1 answers

0
AudioBubble On BEST ANSWER

C# interfaces cannot directly inherit from Lists (because they are classes) - but they can inherit from any interface such as IList or IEnumerable:

Array is a class which inherits from IList and IEnumerable. So you can inherit from IList<T>

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

Or you can inherit from IEnumerable:

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