Class CircularBuffer<T>

A fixed-size circular (ring) buffer that overwrites the oldest elements once full.

Inheritance
CircularBuffer<T>
Namespace: MOSAIC.Components.Basics
Assembly: MOSAIC.dll
Syntax
public class CircularBuffer<T>
Type Parameters
Name Description
T

The type of elements stored in the buffer.

Remarks

Elements are appended sequentially via Add(T). Once the buffer reaches capacity, new elements overwrite the oldest entries. First always returns the oldest element and Last the most recently added one.

This structure is not thread-safe. If concurrent access is required, the caller must synchronize externally.

Constructors

CircularBuffer(int)

Initializes a new instance of the CircularBuffer<T> class with the specified capacity.

Declaration
public CircularBuffer(int capacity)
Parameters
Type Name Description
int capacity

The fixed number of elements the buffer can hold. Must be greater than zero.

Exceptions
Type Condition
ArgumentOutOfRangeException

Thrown when capacity is less than or equal to zero.

Fields

_buffer

Declaration
private readonly T[] _buffer
Field Value
Type Description
T[]

_currentIdx

Declaration
private int _currentIdx
Field Value
Type Description
int

_firstRound

Declaration
private bool _firstRound
Field Value
Type Description
bool

Properties

Capacity

Gets the fixed capacity of the buffer (set at construction time).

Declaration
public int Capacity { get; }
Property Value
Type Description
int

Count

Gets the number of valid elements currently in the buffer.

Declaration
public int Count { get; }
Property Value
Type Description
int
Remarks

Returns the count of elements added so far while filling for the first time. Once the buffer has wrapped at least once, this always equals Capacity.

First

Gets the oldest element in the buffer.

Declaration
public T First { get; }
Property Value
Type Description
T
Exceptions
Type Condition
InvalidOperationException

Thrown when the buffer is empty.

IsFull

Gets a value indicating whether the buffer has wrapped at least once (i.e., Count equals Capacity).

Declaration
public bool IsFull { get; }
Property Value
Type Description
bool

Last

Gets the most recently added element in the buffer.

Declaration
public T Last { get; }
Property Value
Type Description
T
Exceptions
Type Condition
InvalidOperationException

Thrown when the buffer is empty.

Methods

Add(T)

Appends a value to the buffer, overwriting the oldest element if the buffer is full.

Declaration
public void Add(T value)
Parameters
Type Name Description
T value

The value to append.

Clear()

Resets the buffer to its initial empty state and zeroes all array slots.

Declaration
public void Clear()
Remarks

Use this instead of Reset() when T is a reference type, and you want to release references for garbage collection immediately.

Reset()

Resets the buffer to its initial empty state.

Declaration
public void Reset()
Remarks

Existing array slots are not cleared; they will be overwritten by subsequent Add(T) calls. If T is a reference type and deterministic release of old references is needed, use Clear() instead.

Search documentation

Start typing to search the documentation.