Gorgon
Show / Hide Table of Contents

Class GorgonIReadOnlyListExtensions

Extension methods for items that were inexplicably omitted from the IReadOnlyList<T> interface.

Inheritance
object
GorgonIReadOnlyListExtensions
Inherited Members
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
Namespace: Gorgon.Collections
Assembly: Gorgon.Core.dll
Syntax
public static class GorgonIReadOnlyListExtensions

Methods

| Edit this page View Source

Contains<T>(IReadOnlyList<T>, T)

Function to determine if an item of type T exists within the list.

Declaration
public static bool Contains<T>(this IReadOnlyList<T> list, T item)
Parameters
Type Name Description
IReadOnlyList<T> list

The list to evaluate.

T item

The item to find in the list.

Returns
Type Description
bool

true if the item was found, or false if not.

Type Parameters
Name Description
T
Remarks

This method scans through a IReadOnlyList<T> to locate the specified item. If the item is found, then true is returned, and if not, then false is returned instead.

The search will use a native implementation of the Contains method on the list concrete type if available. Otherwise, if the type, T implements IEquatable<T> then that is used for comparing items the list. If that interface is not available, then IComparable<T> is used, and failing that, the Equals(object) method is used to determine equality between the items in the list.

For best performance, it is best to use a type that natively Contains in its concrete implementation.

Exceptions
Type Condition
ArgumentNullException

Thrown when the list parameter is null.

| Edit this page View Source

CopyTo<T>(IReadOnlyList<T>, T[])

Function to copy the contents of this read only list into an array.

Declaration
public static void CopyTo<T>(this IReadOnlyList<T> list, T[] array)
Parameters
Type Name Description
IReadOnlyList<T> list

The list to evaluate.

T[] array

The array that will receive the data.

Type Parameters
Name Description
T

The type of data to copy.

Exceptions
Type Condition
ArgumentNullException

Thrown when the list, or the array parameter is null.

ArgumentException

Thrown when the array isn't large enough to support the entire list.

| Edit this page View Source

FirstIndexOf<T>(IReadOnlyList<T>, Predicate<T>)

Function to find the first index of an item using a predicate to filter through the list.

Declaration
public static int FirstIndexOf<T>(this IReadOnlyList<T> list, Predicate<T> predicate)
Parameters
Type Name Description
IReadOnlyList<T> list

The list to evaluate.

Predicate<T> predicate

The predicate function used to evaluate the list items.

Returns
Type Description
int

The index of the filtered item, or -1 if not found.

Type Parameters
Name Description
T

The type of values in the list.

Exceptions
Type Condition
ArgumentNullException

Thrown when the list, or the predicate parameter is null.

| Edit this page View Source

IndexOf<T>(IReadOnlyList<T>, T)

Function to return the index of an item in a IReadOnlyList<T>.

Declaration
public static int IndexOf<T>(this IReadOnlyList<T> list, T item)
Parameters
Type Name Description
IReadOnlyList<T> list

The list of items to evaluate.

T item

The item to search for.

Returns
Type Description
int

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

Type Parameters
Name Description
T

The type of object in the collection.

Remarks

This method scans through a IReadOnlyList<T> to locate the specified item. If the item is found, the index of that item within the list is returned. Otherwise, if it is not found, -1 is returned.

The search will use a native implementation of the IndexOf method on the list concrete type if available. Otherwise, if the type, T implements IEquatable<T> then that is used for comparing items the list. If that interface is not available, then IComparable<T> is used, and failing that, the Equals(object) method is used to determine equality between the items in the list.

For best performance, it is best to use a type that natively IndexOf in its concrete implementation.

Exceptions
Type Condition
ArgumentNullException

Thrown when the list parameter is null.

| Edit this page View Source

LastIndexOf<T>(IReadOnlyList<T>, Predicate<T>)

Function to find the last index of an item using a predicate to filter through the list.

Declaration
public static int LastIndexOf<T>(this IReadOnlyList<T> list, Predicate<T> predicate)
Parameters
Type Name Description
IReadOnlyList<T> list

The list to evaluate.

Predicate<T> predicate

The predicate function used to evaluate the list items.

Returns
Type Description
int

The index of the filtered item, or -1 if not found.

Type Parameters
Name Description
T

The type of values in the list.

Exceptions
Type Condition
ArgumentNullException

Thrown when the list, or the predicate parameter is null.

  • Edit this page
  • View Source
In this article
Back to top Copyright 2023 - Licensed under the MIT license by Michael Winsor (Tape_Worm).
Send comments on this topic to the author