Queue that only excepts N items

I am trying to keep a list of the last 10 pages a user has been too. I tried to use Stack but it doesn’t support a fixed length so here is a dirty little class to only keep the last N items. Yes I know its a bit dirty!

public class NStack<T>
	{
		public int MaxEntries { get; set; }
		public List<T> Items = new List<T>();
		public void Add(T value)
		{
			if (Items.Count == MaxEntries)
			{
				//remove last item
				Items.RemoveAt(MaxEntries - 1);
			}
			Items.Insert(0, value);
		}
	}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.