Skip to content

ArrayBuilder

A lightweight, pool-backed builder for constructing arrays. Dispose returns the backing buffer to the pool.

C# Signature
internal readonly struct ArrayBuilder<T> : System.IDisposable

The following example demonstrates how to add, remove, consume, and dispose the array builder.

C#
using System;
using System.Reflection;
using Hertzole.SourceGen;
public class MethodCaller
{
public void CallMethods(object target)
{
// Must dispose when the builder is no longer needed.
using ArrayBuilder<MethodInfo> builder = new ArrayBuilder<MethodInfo>();
CollectMethods(target, builder);
CallCollectedMethods(target, builder.AsSpan());
}
private void CollectMethods(object target, ArrayBuilder<MethodInfo> builder)
{
Type type = target.GetType();
MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
builder.AddRange(methods);
}
private void CallCollectedMethods(object target, ReadOnlySpan<MethodInfo> methods)
{
foreach (MethodInfo method in methods)
{
// Methods like object.Equals(object) take parameters, so skip them.
if (method.GetParameters().Length == 0)
{
method.Invoke(target, null);
}
}
}
}

The following example shows how you can use ArrayBuilder to quickly create strings.

C#
using System;
using Hertzole.SourceGen;
public class Announcer
{
public void AnnounceName(int playerIndex)
{
// Must dispose when the builder is no longer needed.
using ArrayBuilder<char> builder = new ArrayBuilder<char>();
builder.AddRange("Player ");
builder.AddRange(playerIndex.ToString());
// ToString on ArrayBuilder<char> returns a constructed string.
Console.WriteLine($"Welcome, {builder.ToString()}!");
}
}

Creates a new ArrayBuilder<T> with a default initial capacity.

C# Signature
public ArrayBuilder();

Creates a new ArrayBuilder<T> with the specified initial capacity.

C# Signature
public ArrayBuilder(int capacity);

The minimum capacity of the backing array.

Creates a new ArrayBuilder<T> initialized with the specified items.

C# Signature
public ArrayBuilder(ReadOnlySpan<T> items);

The items to initialize the builder with.

C# Signature
public int Count { get; }
C# Signature
public T this[int index] { get; set; }

Adds an item to the builder.

C# Signature
public void Add(T item);

The item to add.

Adds a range of items to the builder.

C# Signature
public void AddRange(ReadOnlySpan<T> items);

The items to add.

Adds a range of items to the builder.

C# Signature
public void AddRange(IEnumerable<T> items);

The items to add.

Returns a read-only span over the builder’s contents.

C# Signature
public ReadOnlySpan<T> AsSpan();

A read-only span over the builder’s contents.

Removes all items from the builder.

C# Signature
public void Clear();

Checks if the specified item is added to the array.

C# Signature
public bool Contains(T item);

The item to check for.

true if the item was found; otherwise false.

Copies the contents of this array builder to the specified array.

C# Signature
public void CopyTo(T[] destination, int destinationIndex);

The array to copy the contents to.

The index into the destination array to which the first copied element is written.

Returns the backing buffer to the pool.

C# Signature
public void Dispose();

Gets the index of an item in the array.

C# Signature
public int IndexOf(T item);

The item to check for.

The index of the item in the array, or -1 if the item was not found.

Inserts an item into the specified index and shifts the items after it to the right.

C# Signature
public void Insert(int index, T item);

The zero-based index at which to insert the item.

The item to insert.

Inserts multiple items into the specified index and shifts the items after the newly inserted items to the right.

C# Signature
public void InsertRange(int index, ReadOnlySpan<T> items);

The zero-based index at which to insert the items.

The items to insert.

Inserts multiple items into the specified index and shifts the items after the newly inserted items to the right.

C# Signature
public void InsertRange(int index, IEnumerable<T> items);

The zero-based index at which to insert the items.

The items to insert.

Removes the first occurrence of the specified item from the builder.

C# Signature
public bool Remove(T item);

The item to remove.

true if the item was found and removed; otherwise false.

Removes the item at the specified index.

C# Signature
public void RemoveAt(int index);

The zero-based index of the item to remove.

Removes a range of items, starting at the specified start index and removes the specified count.

C# Signature
public void RemoveRange(int start, int count);

The zero-based index of the first item to remove.

The number of items to remove.

Replaces all values of the specified value in the array with the new specified value.

C# Signature
public void ReplaceAll(T toReplace, T newValue);

The value to replace.

The new value to insert.

Returns a new array with the builder’s contents.

C# Signature
public T[] ToArray();

A new array with the builder’s contents.

Returns a new immutable array with the builder’s contents.

C# Signature
public ImmutableArray<T> ToImmutableArray();

A new immutable array with the builder’s contents.

For ArrayBuilder<char>, returns a new string that represents the builder’s contents. Otherwise, returns a string with the name of the type and the number of elements.

C# Signature
public override string ToString();