ArrayBuilder
A lightweight, pool-backed builder for constructing arrays. Dispose returns the backing buffer to the pool.
internal readonly struct ArrayBuilder<T> : System.IDisposableExamples
Section titled “Examples”The following example demonstrates how to add, remove, consume, and dispose the array builder.
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.
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()}!"); }}Constructors
Section titled “Constructors”ArrayBuilder()
Section titled “ArrayBuilder()”Creates a new ArrayBuilder<T> with a default initial capacity.
public ArrayBuilder();ArrayBuilder(int)
Section titled “ArrayBuilder(int)”Creates a new ArrayBuilder<T> with the specified initial capacity.
public ArrayBuilder(int capacity);capacity
Section titled “capacity”The minimum capacity of the backing array.
ArrayBuilder(ReadOnlySpan<T>)
Section titled “ArrayBuilder(ReadOnlySpan<T>)”Creates a new ArrayBuilder<T> initialized with the specified items.
public ArrayBuilder(ReadOnlySpan<T> items);The items to initialize the builder with.
Properties
Section titled “Properties”public int Count { get; }Item[]
Section titled “Item[]”public T this[int index] { get; set; }Methods
Section titled “Methods”Add(T)
Section titled “Add(T)”Adds an item to the builder.
public void Add(T item);The item to add.
AddRange(ReadOnlySpan<T>)
Section titled “AddRange(ReadOnlySpan<T>)”Adds a range of items to the builder.
public void AddRange(ReadOnlySpan<T> items);The items to add.
AddRange(IEnumerable<T>)
Section titled “AddRange(IEnumerable<T>)”Adds a range of items to the builder.
public void AddRange(IEnumerable<T> items);The items to add.
AsSpan()
Section titled “AsSpan()”Returns a read-only span over the builder’s contents.
public ReadOnlySpan<T> AsSpan();Returns
Section titled “Returns”A read-only span over the builder’s contents.
Clear()
Section titled “Clear()”Removes all items from the builder.
public void Clear();Contains(T)
Section titled “Contains(T)”Checks if the specified item is added to the array.
public bool Contains(T item);The item to check for.
Returns
Section titled “Returns”true if the item was found; otherwise false.
CopyTo(T[], int)
Section titled “CopyTo(T[], int)”Copies the contents of this array builder to the specified array.
public void CopyTo(T[] destination, int destinationIndex);destination
Section titled “destination”The array to copy the contents to.
destinationIndex
Section titled “destinationIndex”The index into the destination array to which the first copied element is written.
Dispose()
Section titled “Dispose()”Returns the backing buffer to the pool.
public void Dispose();IndexOf(T)
Section titled “IndexOf(T)”Gets the index of an item in the array.
public int IndexOf(T item);The item to check for.
Returns
Section titled “Returns”The index of the item in the array, or -1 if the item was not found.
Insert(int, T)
Section titled “Insert(int, T)”Inserts an item into the specified index and shifts the items after it to the right.
public void Insert(int index, T item);The zero-based index at which to insert the item.
The item to insert.
InsertRange(int, ReadOnlySpan<T>)
Section titled “InsertRange(int, ReadOnlySpan<T>)”Inserts multiple items into the specified index and shifts the items after the newly inserted items to the right.
public void InsertRange(int index, ReadOnlySpan<T> items);The zero-based index at which to insert the items.
The items to insert.
InsertRange(int, IEnumerable<T>)
Section titled “InsertRange(int, IEnumerable<T>)”Inserts multiple items into the specified index and shifts the items after the newly inserted items to the right.
public void InsertRange(int index, IEnumerable<T> items);The zero-based index at which to insert the items.
The items to insert.
Remove(T)
Section titled “Remove(T)”Removes the first occurrence of the specified item from the builder.
public bool Remove(T item);The item to remove.
Returns
Section titled “Returns”true if the item was found and removed; otherwise false.
RemoveAt(int)
Section titled “RemoveAt(int)”Removes the item at the specified index.
public void RemoveAt(int index);The zero-based index of the item to remove.
RemoveRange(int, int)
Section titled “RemoveRange(int, int)”Removes a range of items, starting at the specified start index and removes the specified count.
public void RemoveRange(int start, int count);The zero-based index of the first item to remove.
The number of items to remove.
ReplaceAll(T, T)
Section titled “ReplaceAll(T, T)”Replaces all values of the specified value in the array with the new specified value.
public void ReplaceAll(T toReplace, T newValue);toReplace
Section titled “toReplace”The value to replace.
newValue
Section titled “newValue”The new value to insert.
ToArray()
Section titled “ToArray()”Returns a new array with the builder’s contents.
public T[] ToArray();Returns
Section titled “Returns”A new array with the builder’s contents.
ToImmutableArray()
Section titled “ToImmutableArray()”Returns a new immutable array with the builder’s contents.
public ImmutableArray<T> ToImmutableArray();Returns
Section titled “Returns”A new immutable array with the builder’s contents.
ToString()
Section titled “ToString()”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.
public override string ToString();