KGy SOFT

ArraySectionT Structure

KGy SOFT Core Libraries Help
Represents a one dimensional array or a section of an array. This type is very similar to ArraySegmentT/Memory<T> types but can be used on every platform in the same way and it is faster than Memory<T> in most cases. Depending on the used platform it supports ArrayPool allocation.
See the Remarks section for details.

Namespace:  KGySoft.Collections
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.4.0
Syntax

[SerializableAttribute]
public struct ArraySection<T> : IList<T>, 
	ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection, 
	IEquatable<ArraySection<T>>, IReadOnlyList<T>, IReadOnlyCollection<T>

Type Parameters

T
The type of the elements in the collection.

The ArraySectionT type exposes the following members.

Constructors

  NameDescription
Public methodArraySectionT(T)
Initializes a new instance of the ArraySectionT struct from the specified array. No heap allocation occurs when using this constructor overload.
Public methodArraySectionT(Int32, Boolean)
Initializes a new instance of the ArraySectionT struct using an internally allocated buffer.
When using this overload, the returned ArraySectionT instance must be released by the Release method if it is not used anymore.
Public methodArraySectionT(T, Int32)
Initializes a new instance of the ArraySectionT struct from the specified array using the specified offset. No heap allocation occurs when using this constructor overload.
Public methodArraySectionT(T, Int32, Int32)
Initializes a new instance of the ArraySectionT struct from the specified array using the specified offset and length. No heap allocation occurs when using this constructor overload.
Top
Properties

  NameDescription
Public propertyAsArraySegment
Returns the current ArraySectionT instance as an ArraySegmentT.
Public propertyAsMemory
Returns the current ArraySectionT instance as a Memory instance.
Public propertyAsSpan
Returns the current ArraySectionT instance as a Span instance.
Public propertyIsNull
Gets whether this ArraySectionT instance represents a  array.
Please note that the ToArray method returns  when this property returns .
Public propertyIsNullOrEmpty
Gets whether this ArraySectionT instance represents an empty array section or a  array.
Public propertyItem
Gets or sets the element at the specified index.
To return a reference to an element use the GetElementReference method instead.
Public propertyLength
Gets the number of elements in this ArraySectionT.
Top
Methods

  NameDescription
Public methodAsArray2D
Gets this ArraySectionT as an Array2DT instance using the specified height and width. The ArraySectionT must have enough capacity for the specified dimensions.
Public methodAsArray3D
Gets this ArraySectionT as an Array3DT instance using the specified height and width. The ArraySectionT must have enough capacity for the specified dimensions.
Public methodClear
Clears the items in this ArraySectionT instance so all elements will have the default value of type T.
Public methodContains
Determines whether this ArraySectionT contains the specific item.
Public methodCopyTo(T, Int32)
Copies the items of this ArraySectionT to a compatible one-dimensional array, starting at a particular index.
Public methodCopyTo(ArraySectionT, Int32)
Copies the items of this ArraySectionT to a compatible instance, starting at a particular index.
Public methodEquals(Object)
Determines whether the specified object is equal to this instance.
(Overrides ValueTypeEquals(Object).)
Public methodEquals(ArraySectionT)
Indicates whether the current ArraySectionT instance is equal to another one specified in the other parameter.
Public methodGetElementReference
Gets the reference to the element at the specified index.
Public methodGetEnumerator
Returns an enumerator that iterates through the items of this ArraySectionT.
Public methodGetHashCode
Returns a hash code for this ArraySectionT instance.
(Overrides ValueTypeGetHashCode.)
Public methodGetPinnableReference
Returns a reference to the first element in this ArraySectionT. This makes possible to use the ArraySectionT in a fixed statement.
Public methodIndexOf
Determines the index of a specific item in this ArraySectionT.
Public methodRelease
Releases the underlying array. If this ArraySectionT instance was instantiated by the self allocating constructor, then this method must be called when the ArraySectionT is not used anymore. On platforms that do not support the ArrayPool class this method simply sets the self instance to Null.
Public methodSlice(Int32)
Gets a new ArraySectionT instance, which represents a subsection of the current instance with the specified startIndex.
Public methodSlice(Int32, Int32)
Gets a new ArraySectionT instance, which represents a subsection of the current instance with the specified startIndex and length.
Public methodToArray
Copies the elements of this ArraySectionT to a new array.
Top
Operators

  NameDescription
Public operatorStatic memberEquality
Determines whether two specified ArraySectionT instances have the same value.
Public operatorStatic member(T to ArraySectionT)
Performs an implicit conversion from array of T to ArraySectionT.
Public operatorStatic member(ArraySectionT to SpanT)
Performs an implicit conversion from ArraySectionT to Span<T>.
Public operatorStatic memberInequality
Determines whether two specified ArraySectionT instances have different values.
Top
Fields

  NameDescription
Public fieldStatic memberEmpty
Represents the empty ArraySectionT. This field is read-only.
Public fieldStatic memberNull
Represents the  ArraySectionT. This field is read-only.
Top
Extension Methods

  NameDescription
Public Extension MethodAddRangeT (Defined by CollectionExtensions.)
Public Extension MethodAsThreadSafeTOverloaded. (Defined by ListExtensions.)
Public Extension MethodAsThreadSafeTOverloaded. (Defined by CollectionExtensions.)
Public Extension MethodConvert(Type, CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.
(Defined by ObjectExtensions.)
Public Extension MethodCode exampleConvertTTarget(CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)
Public Extension MethodForEachT
Similarly to the List<T>.ForEach method, processes an action on each element of an enumerable collection.
(Defined by EnumerableExtensions.)
Public Extension MethodGetRandomElementT(Boolean)Overloaded.
Gets a random element from the enumerable source using a new FastRandom instance.
(Defined by EnumerableExtensions.)
Public Extension MethodGetRandomElementT(Random, Boolean)Overloaded.
Gets a random element from the enumerable source using a specified Random instance.
(Defined by EnumerableExtensions.)
Public Extension MethodIn (Defined by ObjectExtensions.)
Public Extension MethodIndexOf(FuncObject, Boolean)Overloaded.
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions.)
Public Extension MethodIndexOf(Object)Overloaded.
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions.)
Public Extension MethodIndexOfT(FuncT, Boolean)Overloaded.
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions.)
Public Extension MethodIndexOfT(T)Overloaded.
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions.)
Public Extension MethodInsertRangeT (Defined by ListExtensions.)
Public Extension MethodIsNullOrEmptyOverloaded.
Determines whether the specified source is  or empty (has no elements).
(Defined by EnumerableExtensions.)
Public Extension MethodIsNullOrEmptyTOverloaded.
Determines whether the specified source is  or empty (has no elements).
(Defined by EnumerableExtensions.)
Public Extension MethodJoinT(String)Overloaded.
Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions.)
Public Extension MethodJoinT(Char)Overloaded.
Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions.)
Public Extension MethodRemoveRangeT
Removes count amount of items from the specified collection at the specified index.
(Defined by ListExtensions.)
Public Extension MethodReplaceRangeT
Removes count amount of items from the target IListT at the specified index, and inserts the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by ListExtensions.)
Public Extension MethodShuffleTOverloaded.
Shuffles an enumerable source (randomizes its elements) using a new FastRandom instance.
(Defined by EnumerableExtensions.)
Public Extension MethodShuffleT(Int32)Overloaded.
Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions.)
Public Extension MethodShuffleT(Random)Overloaded.
Shuffles an enumerable source (randomizes its elements) using a specified Random instance.
(Defined by EnumerableExtensions.)
Public Extension MethodToCircularListT (Defined by EnumerableExtensions.)
Public Extension MethodTryAdd(Object, Boolean, Boolean)Overloaded.
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryAddT(T, Boolean, Boolean)Overloaded.
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryAddRange(IEnumerable, Boolean, Boolean)Overloaded.
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryAddRangeT(IEnumerableT, Boolean, Boolean)Overloaded.
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryClear(Boolean, Boolean)Overloaded.
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryClearT(Boolean, Boolean)Overloaded.
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryConvert(Type, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, CultureInfo, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(CultureInfo, TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryGetElementAt(Int32, Object, Boolean, Boolean)Overloaded.
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryGetElementAtT(Int32, T, Boolean, Boolean)Overloaded.
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsert(Int32, Object, Boolean, Boolean)Overloaded.
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsertT(Int32, T, Boolean, Boolean)Overloaded.
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsertRange(Int32, IEnumerable, Boolean, Boolean)Overloaded.
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsertRangeT(Int32, IEnumerableT, Boolean, Boolean)Overloaded.
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemove(Object, Boolean, Boolean)Overloaded.
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveT(T, Boolean, Boolean)Overloaded.
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveAt(Int32, Boolean, Boolean)Overloaded.
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveAtT(Int32, Boolean, Boolean)Overloaded.
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveRange(Int32, Int32, Boolean, Boolean)Overloaded.
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveRangeT(Int32, Int32, Boolean, Boolean)Overloaded.
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions.)
Public Extension MethodTryReplaceRange(Int32, Int32, IEnumerable, Boolean, Boolean)Overloaded.
Tries to remove count amount of items from the target at the specified index, and to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions.)
Public Extension MethodTryReplaceRangeT(Int32, Int32, IEnumerableT, Boolean, Boolean)Overloaded.
Tries to remove count amount of items from the target at the specified index, and to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions.)
Public Extension MethodTrySetElementAt(Int32, Object, Boolean, Boolean)Overloaded.
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTrySetElementAtT(Int32, T, Boolean, Boolean)Overloaded.
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Top
Remarks

The ArraySectionT type is similar to the combination of the Memory type and the .NET Core version of the ArraySegmentT type.

In .NET Core 3.0/.NET Standard 2.1 and above an ArraySectionT instance can be easily turned to a Span instance (either by cast or by the AsSpan property), which is much faster than using the Span property of a Memory instance.

If an ArraySectionT is created by the constructor with a specified size, then depending on the size and the current platform the underlying array might be obtained by using the ArrayPool.

Note Note
An ArraySectionT instance that was instantiated by the self allocating constructor must be released by calling the Release method when it is not used anymore. The ArraySectionT type does not implement IDisposable because releasing is not required when ArraySectionT is created from an existing array but not calling it when it would be needed may lead to decreased application performance.

Though ArraySectionT is practically immutable (has only readonly fields) it is not marked as readonly, which is needed for the Release method to work properly. As ArraySectionT is a non-readonly struct it is not recommended to use it as a readonly field; otherwise, accessing its members would make the compiler to create a defensive copy, which leads to a slight performance degradation.

See Also

Reference