How do I override List's Add method in C#?

后端 未结 10 1295
名媛妹妹
名媛妹妹 2020-11-27 18:59

I am currently looking to make my own collection, which would be just like a regular list, except that it would only hold 10 items. If an item was added when there were alre

10条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 19:17

    Your description of your requirement sounds like a Circular Buffer.

    I implemented my own - similar to this implementation on CodePlex except that mine implements IList.

    Some of the other answers suggest using a Queue - but this isn't quite the same thing, as it only allows FIFO access.

    As a general point, it's not recommended to derive from List - instead derive from Collection, and implement any additional stuff you need. But for a circular buffer it's probably more appropriate to use a private array rather than deriving from Collection like the CodePlex implementation.

提交回复
热议问题