> For the complete documentation index, see [llms.txt](https://pahutyakv.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pahutyakv.gitbook.io/docs/mega-extension-pack/collections.md).

# Collections

#### **Get Element**

* `Get<T>(int index)` — Returns the element at the specified index with bounds validation.
* `First<T>()` — Returns the first element of the source or the default item if the source is empty.
* `Last<T>()` — Returns the last element of the source or the default item if the source is empty.
* `FirstNotNull<T>()` — Returns the first non-null element or default if all are null.
* `LastNotNull<T>()` — Returns the last non-null element of the source or default if all elements are null.
* `RandomElement<T>()` — Returns a random element from the source.
* `RandomElements<T>(int count)` — Returns a new list containing a specified amount of unique random elements.
* `After<T>(T element)` — Returns the element that comes after the specified one, or default if it is the last.
* `Before<T>(T element)` — Returns the element that comes before the specified one, or default if it is the first.
* `NearestTo<T>(T target)` — Returns the element in the source that is closest to the specified target item. Works with numeric types, vector types, and components by comparing their positions.

***

#### **Get Modified Collection**

* `Distincted<T>()` — Returns a new source containing distinct elements from the source.
* `Reversed<T>()` — Returns a new source containing the elements of the original source in reverse order.
* `Shuffled<T>(int iterations = 1)` — Returns a new list containing shuffled elements of the source.
* `Chunk<T>(int size)` — Splits the source into chunks of a specified size.

***

#### **Add/Modify**

* `AddIf<T>(T item, Func<T, bool> predicate)` — Adds an item to the source if it matches a predicate.
* `AddIfNotNull<T>(T item)` — Adds an item if it is not null.
* `Resize<T>(int newSize, T defaultValue = default)` — Resizes the source to the specified size, filling with default values if needed.
* `SwapElementsPosition<T>(int indexOfFirstElement, int indexOfSecondElement)` — Swaps the positions of two elements in the list using their indices.
* `Shuffle<T>(int iterations = 1)` — Shuffles the elements of the list in place.
* `MultiplyElements<T>(int count)` — Duplicates every element in the list a specified number of times.
* `RemoveDuplicates<T>(IEqualityComparer<T> comparer = null)` — Removes duplicate elements from the list using an optional comparer.
* `RemoveRandom<T>()` — Removes a random element from the source.

***

#### **For Each Index**

* `ForEachIndex<T>(Action<int> action)` — Executes an action for every index in the sequence.
* `ForEachIndex<T>(Action<T, int> action)` — Executes an action for every item and its index.

***

#### **Sort**

* `SortAscending<T>()` — Sorts the list in ascending order.
* `SortDescending<T>()` — Sorts the list in descending order.

***

#### **Copy To/From**

* `CopyTo<T>(IList<T> destination, bool clearDestinationList = false)` — Copies elements from the source list to the destination list.
* `CopyFrom<T>(IList<T> reference, bool clearSourceList = false)` — Copies elements from the reference list into the current list.

***

#### **Formatting**

* `ToSeparatedString<T>(string separator = ", ")` — Converts the list into a string with a custom separator.

***

#### **Checks**

* `IsNullOrEmpty<T>()` — Checks whether the list is null or contains no elements.
* `IsEmpty<T>()` — Checks whether the list contains no elements.
* `IsNotEmpty<T>()` — Checks whether the list contains at least one element.
