“Interface not implemented” when Returning Derived Type

前端 未结 12 852
野趣味
野趣味 2020-11-27 21:11

The following code:

public interface ISomeData
{
    IEnumerable Data { get; }
}

public class MyData : ISomeData
{
    private List

        
12条回答
  •  误落风尘
    2020-11-27 22:12

    Why not just return a List from your interface ...

    public interface ISomeData
    {
        List Data { get; }
    }
    

    If you know your consumers are going to both iterate over it (IEnumerable) and add to it (IList) then it seems logical to simply return a List<>.

提交回复
热议问题