logo SERENITY
Game Systems Foundation
Search Results for

    Show / Hide Table of Contents

    Class SafeCollectionUtils

    Utility class for safe array and collection operations

    Inheritance
    object
    SafeCollectionUtils
    Inherited Members
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    Namespace: Serenity.Shared.Utils
    Assembly: Serenity.Shared.dll
    Syntax
    public static class SafeCollectionUtils

    Methods

    GetAtIndexOrThrow<T>(T[], int, string)

    Safely gets an element at a specific index, throwing a descriptive exception if out of bounds

    Declaration
    public static T GetAtIndexOrThrow<T>(T[] array, int index, string errorMessage = null)
    Parameters
    Type Name Description
    T[] array

    The array to access

    int index

    The index to access

    string errorMessage

    Custom error message if index is out of bounds

    Returns
    Type Description
    T

    The element at the specified index

    Type Parameters
    Name Description
    T

    Type of array elements

    Exceptions
    Type Condition
    InvalidOperationException

    Thrown when array is null or index is out of bounds

    GetFirstOrDefault<T>(T[], T)

    Safely gets the first element from an array, returning default value if empty or null

    Declaration
    public static T GetFirstOrDefault<T>(T[] array, T defaultValue = default)
    Parameters
    Type Name Description
    T[] array

    The array to get the first element from

    T defaultValue

    Default value to return if array is null or empty

    Returns
    Type Description
    T

    The first element of the array or the default value

    Type Parameters
    Name Description
    T

    Type of array elements

    GetFirstOrThrow<T>(IEnumerable<T>, string)

    Safely gets the first element from a collection, throwing a descriptive exception if empty or null

    Declaration
    public static T GetFirstOrThrow<T>(IEnumerable<T> collection, string errorMessage = null)
    Parameters
    Type Name Description
    IEnumerable<T> collection

    The collection to get the first element from

    string errorMessage

    Custom error message if collection is null or empty

    Returns
    Type Description
    T

    The first element of the collection

    Type Parameters
    Name Description
    T

    Type of collection elements

    Exceptions
    Type Condition
    InvalidOperationException

    Thrown when collection is null or empty

    GetFirstOrThrow<T>(T[], string)

    Safely gets the first element from an array, throwing a descriptive exception if empty or null

    Declaration
    public static T GetFirstOrThrow<T>(T[] array, string errorMessage = null)
    Parameters
    Type Name Description
    T[] array

    The array to get the first element from

    string errorMessage

    Custom error message if array is null or empty

    Returns
    Type Description
    T

    The first element of the array

    Type Parameters
    Name Description
    T

    Type of array elements

    Exceptions
    Type Condition
    InvalidOperationException

    Thrown when array is null or empty

    In this article
    © 2026 Serenity. All Rights Reserved