Click here to Skip to main content
13,296,407 members (54,441 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 10 Dec 2008

A Generic Circular Array

, 10 Dec 2008
Rate this:
Please Sign up or sign in to vote.
An efficient circular array - fixed length first in, last out.


CircularArray<T> is a class that implements a fixed length first in last out 'queue' or buffer. This is useful, for example, if you want to keep, say, the last 30 values in a real-time system. The next value gets put into the array, and then the last value in the array gets pushed out. It does this by using a fixed array, and uses the DivRem math operator to calculate the position of the head and the tail in the array. It's like the array is circular, with a pointer moving around the circle. The only downside is I had to use an internal facade array to get an array in the sequence I wanted, copying out the values; obviously, this isn't great. I can't think of anything else unless I decide to implement something that appears as an array but is actually a linked list or something.


I needed to keep the last X values in a real-time stock tracking application, efficiently, to work out various things. I couldn't find anything like this at all in Generics (I might be wrong. If I am, can someone please tell me about the built-in class available for this?).

Using the code

The constructor specifies the number of elements to hold. Here, I am holding 'Bar' objects:

CircularArray<Bar> _circularArray = new CircularArray<Bar>(periods);

Push a value onto the array using the Push method:


The Get method gets the element X from the head, e.g., Get(0) gets the last value you pushed on, Get(1) gets the second last value you pushed on etc.


The Array method returns an array sequenced from tail to head, e.g., from the oldest value to the newest values that were pushed on to the queue. One use of this may be to use this as input into a TA-LIB technical analysis library, or anything where you will need an array of the last X values in a real-time system. Here is a snippet of the Unit Test of Array.


public void Push()

    CircularArray<int> cq = new CircularArray<int>(5);


    Assert.AreEqual(7, cq.Array[0]);
    Assert.AreEqual(8, cq.Array[1]);
    Assert.AreEqual(9, cq.Array[2]);
    Assert.AreEqual(10, cq.Array[3]);
    Assert.AreEqual(11, cq.Array[4]);


I've included the code and Unit Tests in the solution Zip file; everything is there.

Points of interest

As mentioned, I couldn't find anything like this already available. I've probably missed out on some sort of built-in generic Microsoft class. If anyone knows about such a thing, please let me know!

Update: Note, after I posted this, I did read Marc Clifton's article 'A Circular List'. After plugging it into an NUnit test equivalent to the one above:

public void CicularList()

    CircularList<int> cq = new CircularList<int>(5);

    cq.Value = 1;

    cq.Value = 2;

    cq.Value = 3;

    cq.Value = 4;

    cq.Value = 5;

    cq.Value = 6;

    cq.Value = 7;

    cq.Value = 8;

    cq.Value = 9;

    cq.Value = 10;

    cq.Value = 11;

    Assert.AreEqual(7, cq[0]); // Failed its 11
    Assert.AreEqual(8, cq[1]); // Failed its 7
    Assert.AreEqual(9, cq[2]); // Failed its 8
    Assert.AreEqual(10, cq[3]); // Failed its 9
    Assert.AreEqual(11, cq[4]); // Failed its 10


The array cq would be 11,7,8,9,10, which is the raw circular array. I was expecting 7,8,9,10,11 - e.g., the last 5 values, oldest to newest. Maybe, I'm using it wrong. 'Failed' above is just against what I expected, so don't read into it. I'm sure CircularList is useful for some, just not for my purposes. I totally respect and value Marc Clifton's contributions.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jean-marc Lai
Technical Lead
United Kingdom United Kingdom
Jean-marc is a IT consultant specializing on the microsoft platform. Jean-marc lives in London, UK.

You may also be interested in...

Comments and Discussions

Generalcircular buffers with contiguous storage of values Pin
suralis16-Dec-08 0:40
membersuralis16-Dec-08 0:40 
GeneralRe: circular buffers with contiguous storage of values Pin
Jean-marc Lai17-Dec-08 11:50
memberJean-marc Lai17-Dec-08 11:50 
GeneralThoughts Pin
PIEBALDconsult10-Dec-08 10:29
memberPIEBALDconsult10-Dec-08 10:29 
GeneralRe: Thoughts Pin
Jean-marc Lai11-Dec-08 0:02
memberJean-marc Lai11-Dec-08 0:02 
GeneralRe: Thoughts [modified] Pin
PIEBALDconsult11-Dec-08 9:44
memberPIEBALDconsult11-Dec-08 9:44 
GeneralRe: Thoughts Pin
Jean-marc Lai12-Dec-08 16:20
memberJean-marc Lai12-Dec-08 16:20 
GeneralRe: Thoughts Pin
PIEBALDconsult12-Dec-08 19:11
memberPIEBALDconsult12-Dec-08 19:11 
General_facadeArray problems Pin
Luc Pattyn10-Dec-08 5:37
mvpLuc Pattyn10-Dec-08 5:37 
GeneralRe: _facadeArray problems Pin
Jean-marc Lai10-Dec-08 6:12
memberJean-marc Lai10-Dec-08 6:12 
GeneralRe: _facadeArray problems Pin
Luc Pattyn10-Dec-08 6:51
mvpLuc Pattyn10-Dec-08 6:51 
GeneralRe: _facadeArray problems Pin
Jean-marc Lai11-Dec-08 0:06
memberJean-marc Lai11-Dec-08 0:06 
GeneralRe: _facadeArray problems Pin
Luc Pattyn11-Dec-08 1:16
mvpLuc Pattyn11-Dec-08 1:16 
GeneralRe: _facadeArray problems Pin
Jean-marc Lai11-Dec-08 1:35
memberJean-marc Lai11-Dec-08 1:35 
GeneralRe: _facadeArray problems Pin
Luc Pattyn11-Dec-08 2:36
mvpLuc Pattyn11-Dec-08 2:36 
GeneralGood Article Pin
Donsw10-Dec-08 5:31
memberDonsw10-Dec-08 5:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 10 Dec 2008
Article Copyright 2008 by Jean-marc Lai
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid