Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 297215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:37:10+00:00 2026-05-12T06:37:10+00:00

In the .Net BCL is there a collection data structure similar to list that

  • 0

In the .Net BCL is there a collection data structure similar to list that has a maximum capacity, say configured to 100 items, which when item 101 is added the original first item is popped/removed from the collection thus ensuring the item count never exceeds 100.

I’m using .net 3.5

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:37:10+00:00Added an answer on May 12, 2026 at 6:37 am

    What you are describing is a circular buffer. I use these occasionally and recently ported some older code into a genericised C# class (attached). This code is part of SharpNeat V2 development.

    This has O(1) performance on add and remove oprations whereas the solution posted that encapsulates a List is O(n). This is because removing the 0th item in a list causes all other items to be shuffled down to fill the gap.

    
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace SharpNeat.Utility
    {
        /// 
        /// This is a generic circular buffer of items of type T.  A circular buffer must be assigned
        /// a capacity at construction time. Items can be enqueued indefintely, but when the buffer's 
        /// capacity is reached the oldest values in the buffer are overwritten, thus the buffer is best
        /// thought of as a circular array or buffer.
        /// 
        public class CircularBuffer
        {
            /// 
            /// Internal array that stores the circular buffer's values.
            /// 
            protected T[] _buff;
    
            /// 
            /// The index of the previously enqueued item. -1 if buffer is empty.
            /// 
            protected int _headIdx;
    
            /// 
            /// The index of the next item to be dequeued. -1 if buffer is empty.
            /// 
            protected int _tailIdx;
    
            #region Constructors
    
            /// 
            /// Constructs a circular buffer with the specified capacity.
            /// 
            /// 
            public CircularBuffer(int capacity)
            {
                _buff = new T[capacity];
                _headIdx = _tailIdx = -1;
            }
    
            #endregion
    
            #region Properties
    
            /// 
            /// Gets the number of items in the buffer. Returns the buffer's capacity
            /// if it is full.
            /// 
            public int Length
            {
                get
                {
                    if(_headIdx == -1) 
                        return 0;
    
                    if(_headIdx > _tailIdx)
                        return (_headIdx - _tailIdx) + 1;
    
                    if(_tailIdx > _headIdx)
                        return (_buff.Length - _tailIdx) + _headIdx + 1;
    
                    return 1;
                }
            }
    
            #endregion
    
            #region Public Methods
    
            /// 
            /// Clear the buffer.
            /// 
            public virtual void Clear()
            {
                _headIdx = _tailIdx = -1;
            }
    
            /// 
            /// Enqueue a new item. This overwrites the oldest item in the buffer if the buffer
            /// has reached capacity.
            /// 
            /// 
            public virtual void Enqueue(T item)
            {
                if(_headIdx == -1)
                {   // buffer is currently empty.
                    _headIdx = _tailIdx = 0;
                    _buff[0] = item;
                    return;
                }
    
                // Determine the index to write to.
                if(++_headIdx == _buff.Length)
                {   // Wrap around.
                    _headIdx = 0;
                }
    
                if(_headIdx == _tailIdx)
                {   // Buffer overflow. Increment tailIdx.
                    if(++_tailIdx == _buff.Length) 
                    {   // Wrap around.
                        _tailIdx=0;
                    }
                    _buff[_headIdx] = item;
                    return;
                }
    
                _buff[_headIdx] = item;
                return;
            }
    
            /// 
            /// Remove the oldest item from the back end of the buffer and return it.
            /// 
            /// 
            public virtual T Dequeue()
            {
                if(_tailIdx == -1)
                {   // buffer is currently empty.
                    throw new InvalidOperationException("buffer is empty.");
                }
    
                T item = _buff[_tailIdx];
    
                if(_tailIdx == _headIdx)
                {   // The buffer is now empty.
                    _headIdx=_tailIdx=-1;
                    return item;
                }
    
                if(++_tailIdx == _buff.Length)
                {   // Wrap around.
                    _tailIdx = 0;
                }
    
                return item;
            }
    
            /// 
            /// Pop the most recently added item from the front end of the buffer and return it.
            /// 
            /// 
            public virtual T Pop()
            {
                if(_tailIdx == -1)
                {   // buffer is currently empty.
                    throw new InvalidOperationException("buffer is empty.");
                }   
    
                T item = _buff[_headIdx];
    
                if(_tailIdx == _headIdx)
                {   // The buffer is now empty.
                    _headIdx = _tailIdx =- 1;
                    return item;
                }
    
                if(--_headIdx==-1)
                {   // Wrap around.
                    _headIdx=_buff.Length-1;
                }
    
                return item;
            }
    
            #endregion
        }
    }
    
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a class in the .NET BCL that can store a block of
The base class library in .NET has some excellent data structures for collections (List,
.NET has a lot of complex data structures. Unfortunately, some of them are quite
In the .NET BCL, there is a CurrentThread and a ProcessThread object. What is
In the .NET BCL there are circular references between: System.dll and System.Xml.dll System.dll and
Is there any existing functionality in the .NET BCL to print the full signature
Is there a more appropriate/efficient data structure for a transaction queue than using a
Are there any good examples of the Abstract Factory in the .NET BCL?
Can anyone tell me what version of the .NET framework (CLR and BCL) is
.NET developers out there! Need your opinion here! I am now using Visual Assist

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.