开发者

What to do when class implementing IList<T> requires that I implement Count property?

I have t开发者_如何学JAVAhe following class:

public class GraphingResults : IList<GraphingResult>
{
    public List<GraphingResult> ToolkitResultList { get; set; }

    public GraphingResults()
    {
        this.ToolkitResultList = new List<GraphingResult>();
    }

    // bunch of other stuff
}

As this class implements IList<GraphingResult>, it requires that I implement the following property (among many others!)

public int Count
{
    get { throw new NotImplementedException(); }
}

I'm not sure exactly what to do... do I need to return the count of this.ToolkitResultList?


To throw a question back at you, why are you exposing a List publicly, as well as implemented IList?

The answer is that you should be doing one or the other. ie, Either expose the list publicly as you are doing, and dont implement IList, or dont expose the List, and insteads make your class the interface to that list:

public class GraphingResults : IList<GraphingResult>
{
    private List<GraphingResult> ToolkitResultList { get; set; }

    public GraphingResults()
    {
        this.ToolkitResultList = new List<GraphingResult>();
    }

    // bunch of other stuff

    public int Count
    {
        get { return this.ToolkitResultList.Count; }
    }

}


Yes, if this class is really just a wrapper around ToolkitResultList then that would make the most sense to me.


Your class would act as a proxy for the internally used List, but if this is your indented design, you might change the base class to Collection:

public class GraphingResults : Collection<GraphingResult>
{
}

you find this class in the System.Collections.ObjectModel namespace. It does all the work four you and is especially for cases like your's.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜