CircularBuffer<T> Class

A circular buffer (ring buffer) collection that maintains a fixed-size buffer and overwrites old items when full.

public sealed class CircularBuffer<T> : ICollection<T>, IEnumerable<T>, IReadOnlyCollection<T>, IReadOnlyList<T>, IEnumerable

Type parameters

T

Example

var buffer = new CircularBuffer<int>(3) { AllowOverwrite = true };
buffer.AddLast(1);
buffer.AddLast(2);
buffer.AddLast(3);
Assert.Equal([1, 2, 3], buffer);

buffer.AddLast(4); // overwrites 1
Assert.Equal([2, 3, 4], buffer);
Implements

Nested types

NameDescription
CircularBuffer<T>.Enumerator

Constructors

NameDescription
CircularBuffer(int)Initializes a new instance of the CircularBuffer<T> class with the specified capacity.

CircularBuffer(int)

CircularBuffer.cs

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

public CircularBuffer(int capacity)

Parameters

capacity int

Properties

NameDescription
AllowOverwriteGets or sets a value indicating whether old items should be overwritten when the buffer is full.
CapacityGets or sets the maximum number of items the buffer can hold.
CountGets the current number of items in the buffer.
System.Collections.Generic.ICollection<T>.IsReadOnly

AllowOverwrite

CircularBuffer.cs

Gets or sets a value indicating whether old items should be overwritten when the buffer is full.

public bool AllowOverwrite { get; set; }

Property value

bool

Gets or sets the maximum number of items the buffer can hold.

public int Capacity { get; set; }

Property value

int

Gets the current number of items in the buffer.

public int Count { get; }

Property value

int

System.Collections.Generic.ICollection<T>.IsReadOnly

CircularBuffer.cs
bool ICollection<T>.IsReadOnly { get; }

Property value

bool

No documentation is available.

Indexers

NameDescription
this[int]
public T this[int index] { get; }

Parameters

index int

Property value

T

No documentation is available.

Methods

NameDescription
AddFirst(T)Adds an item to the beginning of the buffer.
AddLast(T)
Clear()
Contains(T)
CopyTo(T[], int)
GetEnumerator()
IndexOf(T)
RemoveFirst()
RemoveLast()
System.Collections.Generic.ICollection<T>.Add(T)
System.Collections.Generic.ICollection<T>.Remove(T)
System.Collections.Generic.IEnumerable<T>.GetEnumerator()
System.Collections.IEnumerable.GetEnumerator()

Adds an item to the beginning of the buffer.

public void AddFirst(T value)

Parameters

value T
public void AddLast(T value)

Parameters

value T

No documentation is available.

public void Clear()

No documentation is available.

public bool Contains(T item)

Parameters

item T

Returns

bool

No documentation is available.

CopyTo(T[], int)

CircularBuffer.cs
public void CopyTo(T[] array, int arrayIndex)

Parameters

array T[]
arrayIndex int

No documentation is available.

GetEnumerator()

CircularBuffer.cs
public CircularBuffer<T>.Enumerator GetEnumerator()

No documentation is available.

public int IndexOf(T item)

Parameters

item T

Returns

int

No documentation is available.

RemoveFirst()

CircularBuffer.cs
public T RemoveFirst()

Returns

T

No documentation is available.

RemoveLast()

CircularBuffer.cs
public T RemoveLast()

Returns

T

No documentation is available.

System.Collections.Generic.ICollection<T>.Add(T)

CircularBuffer.cs
void ICollection<T>.Add(T item)

Parameters

item T

No documentation is available.

System.Collections.Generic.ICollection<T>.Remove(T)

CircularBuffer.cs
bool ICollection<T>.Remove(T item)

Parameters

item T

Returns

bool

No documentation is available.

System.Collections.Generic.IEnumerable<T>.GetEnumerator()

CircularBuffer.cs
IEnumerator<T> IEnumerable<T>.GetEnumerator()

No documentation is available.

System.Collections.IEnumerable.GetEnumerator()

CircularBuffer.cs
IEnumerator IEnumerable.GetEnumerator()

No documentation is available.