Class DynamicArray<T>
Generic growable array.
Namespace: UnityEngine.Rendering
Syntax
public class DynamicArray<T>
where T : new()
Type Parameters
| Name | Description |
|---|---|
| T | Type of the array. |
Constructors
DynamicArray()
Constructor. Defaults to a size of 32 elements.
Declaration
public DynamicArray()
DynamicArray(Int32)
Constructor
Declaration
public DynamicArray(int size)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | size | Number of elements. |
Properties
capacity
Allocated size of the array.
Declaration
public int capacity { get; }
Property Value
| Type | Description |
|---|---|
| Int32 |
Item[Int32]
ref access to an element.
Declaration
public ref T this[int index] { get; }
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | index | Element index |
Property Value
| Type | Description |
|---|---|
| T | The requested element. |
size
Number of elements in the array.
Declaration
public int size { get; }
Property Value
| Type | Description |
|---|---|
| Int32 |
Methods
Add(in T)
Add an element to the array.
Declaration
public int Add(in T value)
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | Element to add to the array. |
Returns
| Type | Description |
|---|---|
| Int32 | The index of the element. |
AddRange(DynamicArray<T>)
Adds the elements of the specified collection to the end of the DynamicArray.
Declaration
public void AddRange(DynamicArray<T> array)
Parameters
| Type | Name | Description |
|---|---|---|
| DynamicArray<T> | array | The array whose elements should be added to the end of the DynamicArray. The array itself cannot be null, but it can contain elements that are null, if type T is a reference type. |
Clear()
Clear the array of all elements.
Declaration
public void Clear()
Contains(T)
Determines whether the DynamicArray contains a specific value.
Declaration
public bool Contains(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The object to locate in the DynamicArray. |
Returns
| Type | Description |
|---|---|
| Boolean | true if item is found in the DynamicArray; otherwise, false. |
FindIndex(Int32, Int32, Predicate<T>)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the DynamicArray that starts at the specified index and contains the specified number of elements.
Declaration
public int FindIndex(int startIndex, int count, Predicate<T> match)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | startIndex | The zero-based starting index of the search. |
| Int32 | count | The number of elements in the section to search. |
| Predicate<T> | match | The Predicate delegate that defines the conditions of the element to search for. |
Returns
| Type | Description |
|---|---|
| Int32 | The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1. |
GetEnumerator()
Returns an enumerator that iterates through of this array. See the IEnumerable docs for more info: https://docs.microsoft.com/en-us/dotnet/api/system.collections.ienumerable
Declaration
public DynamicArray<T>.Iterator GetEnumerator()
Returns
| Type | Description |
|---|---|
| DynamicArray.Iterator<> | Iterator pointing before the first element in the array. |
Remarks
The returned struct intentionally does not explicitly implement the IEnumarable/IEnumerator interfaces it just follows
the same function signatures. This means the duck typing used by foreach on the compiler level will
pick it up as IEnumerable but at the same time avoids generating Garbage.
For more info, see the C# language specification of the foreach statement.
IndexOf(T)
Searches for the specified object and returns the zero-based index of the first occurrence within the entire DynamicArray.
Declaration
public int IndexOf(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The object to locate in the DynamicArray. The value can be null for reference types. |
Returns
| Type | Description |
|---|---|
| Int32 | he zero-based index of the first occurrence of item within the entire DynamicArray, if found; otherwise, -1. |
IndexOf(T, Int32)
Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the DynamicArray that extends from the specified index to the last element.
Declaration
public int IndexOf(T item, int index)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The object to locate in the DynamicArray. The value can be null for reference types. |
| Int32 | index | The zero-based starting index of the search. 0 (zero) is valid in an empty list. |
Returns
| Type | Description |
|---|---|
| Int32 | The zero-based index of the first occurrence of item within the range of elements in the DynamicArray that extends from index to the last element, if found; otherwise, -1. |
IndexOf(T, Int32, Int32)
Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the DynamicArray that starts at the specified index and contains the specified number of elements.
Declaration
public int IndexOf(T item, int index, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The object to locate in the DynamicArray. The value can be null for reference types. |
| Int32 | index | The zero-based starting index of the search. 0 (zero) is valid in an empty list. |
| Int32 | count | The number of elements in the section to search. |
Returns
| Type | Description |
|---|---|
| Int32 |
Remove(T)
Removes the first occurrence of a specific object from the DynamicArray.
Declaration
public bool Remove(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The object to remove from the DynamicArray. The value can be null for reference types. |
Returns
| Type | Description |
|---|---|
| Boolean | true if item is successfully removed; otherwise, false. This method also returns false if item was not found in the DynamicArray. |
RemoveAt(Int32)
Removes the element at the specified index of the DynamicArray.
Declaration
public void RemoveAt(int index)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | index | The zero-based index of the element to remove. |
RemoveRange(Int32, Int32)
Removes a range of elements from the DynamicArray.
Declaration
public void RemoveRange(int index, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | index | The zero-based starting index of the range of elements to remove. |
| Int32 | count | The number of elements to remove. |
Reserve(Int32, Boolean)
Sets the total number of elements the internal data structure can hold without resizing.
Declaration
public void Reserve(int newCapacity, bool keepContent = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | newCapacity | New capacity for the array. |
| Boolean | keepContent | Set to true if you want the current content of the array to be kept. |
Resize(Int32, Boolean)
Resize the Dynamic Array. This will reallocate memory if necessary and set the current size of the array to the provided size.
Declaration
public void Resize(int newSize, bool keepContent = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | newSize | New size for the array. |
| Boolean | keepContent | Set to true if you want the current content of the array to be kept. |
SubRange(Int32, Int32)
Returns an IEnumeralbe-Like object that iterates through a subsection of this array.
Declaration
public DynamicArray<T>.RangeEnumerable SubRange(int first, int numItems)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | first | The index of the first item |
| Int32 | numItems | The number of items to iterate |
Returns
| Type | Description |
|---|---|
| DynamicArray.RangeEnumerable<> |
|
Remarks
The returned struct intentionally does not explicitly implement the IEnumarable/IEnumerator interfaces it just follows
the same function signatures. This means the duck typing used by foreach on the compiler level will
pick it up as IEnumerable but at the same time avoids generating Garbage.
For more info, see the C# language specification of the foreach statement.
Operators
Implicit(DynamicArray<T> to T[])
Implicit conversion to regular array.
Declaration
public static implicit operator T[](DynamicArray<T> array)
Parameters
| Type | Name | Description |
|---|---|---|
| DynamicArray<T> | array | Input DynamicArray. |
Returns
| Type | Description |
|---|---|
| T[] | The internal array. |