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 8848085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:20:42+00:00 2026-06-14T12:20:42+00:00

I am trying to create my own Circular Buffer in C#. I am using

  • 0

I am trying to create my own Circular Buffer in C#. I am using a counter to keep track of where I need to insert the next item. Here is a Link to the full code and here is the (simplified) code to do so:

public class CircularBuffer<T>
{
    private bool _isFull = false;

    private int _size;
    private int _current = 0;

    private BufferItem<T>[] _buffer;

    public CircularBuffer(int size)
    {
        _size = size;
        _buffer = new BufferItem<T>[size];
    }

    public void Insert(T value)
    {
        BufferItem<T> item = new BufferItem<T>(value);
        //Removed code to check if the buffer is full, if so over-write the oldest item
        //and don't insert at the current position
        _buffer[_current] = item;
        _isFull = (_current == (_size - 1));
        _current++;

        //Age all items            
    }

It all works fine and dandy until after I increment the current position and try to add another item:

enter image description here

Here I added the item "first" and the current position (_current) is incremented.

enter image description here

I added the item "third" but the current position gets reset to 0. There is absolutely no other code including _current other than declaring it, accessing it and incrementing it.
What in the world is going on? Here is the code for BufferItem<T>:

public class BufferItem<T>
{
    public int Age = 0;
    public T Item;

    public BufferItem(T item)
    {
        Item = item;
    }
}
  • 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-06-14T12:20:43+00:00Added an answer on June 14, 2026 at 12:20 pm

    Your class methods are fine. Your _current variable works fine, too. The issue lies outside of the CircularBuffer class. Somewhere where this is being implemented, the CircularBuffer object in question is getting reset in some way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create my own next and previous arrows in a fancybox
I'm trying to create my own Media Player application using a WPF MediaElement. I
I am trying to create my own module, i followed the following tutorial: LINK
I'm trying to create my own style of a menu item but haven't managed
I am trying to create my own grid view. here I retrieve the food
I am trying to create my own Native win32 C++ Checkbox that can have
I am trying to create my own lightbox for images on my website. Yes,
I'm trying to create my own Magento shipping module in Magento 1.6. Since I
I am trying to create my own torrent tracker but dont know how to
I'm trying to create my own plugin. But I'm having trouble getting things right.

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.