Search code examples
interfaceilist

How to role usage of c# IList interface type or other interface as type?


IList is a interface. I have just started learning generics. And I know interface is a contract that class implements it promises to use its methods. So I never thought about it using like value type like:

Can someone please explain me what does this and how to think of its usage when using an interface like type:

IList<Writer> someName;

I am creating here generic typ of IList interface. But interface is not a class so how should I imagine it role?

Or am I misunderstanding something?


Solution

  • If a member/variable is declared as an interface, it can be instantiated with any class that implements said contract. For example, you could instantiate someName with List<Writer>(), but not with new IList<Writer>().

    IList<Writer> someName = new List<Writer>();
    // someName can access any member of IList, but not specific members of List