Create a List<sometype> of a certain size

Hi, I am writing to ask if anyone knows of an existing .Net framework 4.5 class that allows a list of a certain size, and when the size is exceeded, it just drops off/loses one off the end keeping the most recent items.

For example

  SomeListClass<int> slc = new SomeListClass<int>(3);  //can contain only 3 items
  slc.Add(1);     //list contains 1
  slc.Add(2);     //list contains 1,2
  slc.Add(3);     //list contains 1, 2, 3
  slc.Add(4);     //list contains 2, 3, 4
  slc.Add(100);//list contains 3, 4, 100

Open in new window


I could easily roll my own, but I don't want to do this if it is already provided by .Net
John BolterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
The Queue<T> generic class enables you to easily remove the oldest element in the collection, but won't automatically do it for you.

You will either have to check if it is "full" each time you enqueue (add) something, or inherit from it and see if it is possible to build the mechanism Inside of you own class. Unfortunately, most of its methods are not overridable (virtual), so it might not be a straightforward thing to do.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
I'm 99% sure that you'll have to roll your own.
0
Fernando SotoRetiredCommented:
Hi John;

Yes as the others have said there is no collection that will do that automatically. As James has stated you could use a Queue object and modify it to fit your needs. Below is a class which Inheritance from the queue Generic class which will do as you stated in your question.

using System.Collections.Generic;

namespace YourNameSpace
{
    class QueueMaxSize<T> : Queue<T>
    {
    	  // The maxium size of your queue
        public int MaxSize { get; set; }

        // Default constructor to set default size
        public QueueMaxSize()
        {
            // Set a default size
            MaxSize = 10;
        }

        // Constructor to accept the max size of the queue
        public QueueMaxSize(int maxsize)
        {
            MaxSize = maxsize;
        }

        // This hides the base classes Enqueue method to implement
        // some modification in the process of adding to the queue
        public new void Enqueue(T item)
        {
            // If the queue has reached the max size remove the first entry
            if(this.Count >= MaxSize)
                this.Dequeue();

            // Add the new item to the queue
            base.Enqueue(item);
        }
    }
}

Open in new window

Then in your code you could do the following.

QueueMaxSize<int> slc = new QueueMaxSize<int>(3);  //can contain only 3 items

private void button4_Click(object sender, EventArgs e)
{
    slc.Enqueue(1);     //list contains 1
    slc.Enqueue(2);     //list contains 1,2
    slc.Enqueue(3);     //list contains 1, 2, 3
    slc.Enqueue(4);     //list contains 2, 3, 4
    slc.Enqueue(100);   //list contains 3, 4, 100
}

Open in new window

0
käµfm³d 👽Commented:
Aww... no fair! He said he'd roll his own. Well, since I was bored, I thought I'd throw out an old-skool approach using arrays.

public class CircularBuffer<T> : IEnumerable<T>
{
    private T[] buffer;
    private int currentHead;

    public CircularBuffer(int size)
    {
        if (size <= 0) throw new ArgumentException("Buffer size must be at least 1.");

        this.buffer = new T[size];
        this.currentHead = 0;
    }

    public void Add(T item)
    {
        this.buffer[this.currentHead++] = item;

        this.currentHead = this.currentHead % this.buffer.Length;   // Wrap around if necessary
    }

    public IEnumerator<T> GetEnumerator()
    {
        for (int i = this.currentHead; i < this.buffer.Length; i++)
        {
            yield return this.buffer[i];
        }

        for (int i = 0; i < this.currentHead; i++)
        {
            yield return this.buffer[i];
        }
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public T this[int index]
    {
        get
        {
            return this.buffer[(this.currentHead + index) % this.buffer.Length];
        }

        set
        {
            this.buffer[(this.currentHead + index) % this.buffer.Length] = value;
        }
    }
}

Open in new window


You can index and iterate the above data structure. I didn't do exhaustive testing, so there could be bugs.
0
John BolterAuthor Commented:
Thanks everyone. This has all been so helpful.
I have a lot to learn !!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.