docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Struct ReadOnlyArray<TValue>

    Read-only access to an array or to a slice of an array.

    Implements
    IReadOnlyList<TValue>
    IReadOnlyCollection<TValue>
    IEnumerable<TValue>
    IEnumerable
    Inherited Members
    ValueType.Equals(object)
    ValueType.GetHashCode()
    ValueType.ToString()
    Namespace: UnityEngine.InputSystem.Utilities
    Assembly: Unity.InputSystem.dll
    Syntax
    public struct ReadOnlyArray<TValue> : IReadOnlyList<TValue>, IReadOnlyCollection<TValue>, IEnumerable<TValue>, IEnumerable
    Type Parameters
    Name Description
    TValue

    Type of values stored in the array.

    Remarks

    The purpose of this struct is to allow exposing internal arrays directly such that no boxing and no going through interfaces is required but at the same time not allowing the internal arrays to be modified.

    It differs from ReadOnlySpan<T> in that it can be stored on the heap and differs from ReadOnlyCollection<T> in that it supports slices directly without needing an intermediate object representing the slice.

    Note that in most cases, the ReadOnlyArray instance should be treated as a temporary. The actual array referred to by a ReadOnlyArray instance is usually owned and probably mutated by another piece of code. When that code makes changes to the array, the ReadOnlyArray instance will not get updated.

    Constructors

    ReadOnlyArray(TValue[])

    Construct a read-only array covering all of the given array.

    Declaration
    public ReadOnlyArray(TValue[] array)
    Parameters
    Type Name Description
    TValue[] array

    Array to index.

    ReadOnlyArray(TValue[], int, int)

    Construct a read-only array that covers only the given slice of array.

    Declaration
    public ReadOnlyArray(TValue[] array, int index, int length)
    Parameters
    Type Name Description
    TValue[] array

    Array to index.

    int index

    Index at which to start indexing array. The given element becomes index #0 for the read-only array.

    int length

    Length of the slice to index from array.

    Properties

    Count

    Number of elements in the array.

    Declaration
    public int Count { get; }
    Property Value
    Type Description
    int

    this[int]

    Return the element at the given index.

    Declaration
    public TValue this[int index] { get; }
    Parameters
    Type Name Description
    int index

    Index into the array.

    Property Value
    Type Description
    TValue
    Exceptions
    Type Condition
    IndexOutOfRangeException

    index is less than 0 or greater than Count.

    InvalidOperationException

    Methods

    GetEnumerator()

    Returns an enumerator that iterates through the read-only array. ReadOnlyArray<TValue>.Enumerator An enumerator for the read-only array.

    Declaration
    public ReadOnlyArray<TValue>.Enumerator GetEnumerator()
    Returns
    Type Description
    ReadOnlyArray<TValue>.Enumerator

    IndexOf(Predicate<TValue>)

    Searches for the first element in the array for which the given predicate is true and returns the index of that element.

    Declaration
    public int IndexOf(Predicate<TValue> predicate)
    Parameters
    Type Name Description
    Predicate<TValue> predicate

    The predicate to be evaluated for each element which defines the condition for the search.

    Returns
    Type Description
    int

    Index of the first element for which predicate is truex or -1 if no such element exists.

    Examples
    // Searches for the first element in an integer array that is greater or equal to 5.
    var haystack = new ReadOnlyArray<int>(new[] { 1, 2, 3, 4, 5, 6, 7 });
    var index = haystack.IndexOf((value) => value >= 5); // index == 4
    Exceptions
    Type Condition
    ArgumentNullException

    If predicate is null.

    ToArray()

    Convert to array.

    Declaration
    public TValue[] ToArray()
    Returns
    Type Description
    TValue[]

    A new array containing a copy of the contents of the read-only array.

    Operators

    implicit operator ReadOnlyArray<TValue>(TValue[])

    Constructs a read-only array containing elements array.

    Declaration
    public static implicit operator ReadOnlyArray<TValue>(TValue[] array)
    Parameters
    Type Name Description
    TValue[] array

    An existing array containing elements to be wrapped as a read-only array.

    Returns
    Type Description
    ReadOnlyArray<TValue>

    Implements

    IReadOnlyList<T>
    IReadOnlyCollection<T>
    IEnumerable<T>
    IEnumerable
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)