How do I define the return type of an interface method to be another interface?

后端 未结 6 1498
陌清茗
陌清茗 2021-02-02 18:09

I\'m new to interfaces and abstract classes. I want to create a couple of interfaces to define core methods and variables for the objects for a shopping cart system. Then I want

6条回答
  •  情歌与酒
    2021-02-02 18:56

    An interface is an agreement. A contract, if you will, between you and anyone who uses your class. By telling folks that you are implementing the ICart interface, you are promising them that all the methods in the interface exist on your class. Thus, all your methods must match the signatures of the interface methods.

    In order to return of list of items that implement ICartItem, you need to use generics as suggested by DrJokepu. This tells everyone that the interface only gurentees a list of objects that implement ICartItem, not ICartItem specifically.

    public interface ICart where T : ICartItem
    {
        List CartItems { get; set; }
    }
    

提交回复
热议问题