KGy SOFT

Array2DT Structure

KGy SOFT Core Libraries Help
Represents a rectangular array, whose indexer access is faster than a regular 2D array. It supports accessing its rows or the whole content as a single dimensional ArraySectionT. Depending on the used platform it supports ArrayPool allocation and casting to Span.
See the Remarks section for details.

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

[SerializableAttribute]
public struct Array2D<T> : IDisposable, IEquatable<Array2D<T>>, 
	IEnumerable<T>, IEnumerable

Type Parameters

T
The type of the elements in the collection.

The Array2DT type exposes the following members.

Constructors

  NameDescription
Public methodArray2DT(Int32, Int32)
Initializes a new instance of the Array2DT struct using the specified height and width. Parameter order is the same as in case of instantiating a regular two-dimensional array.
If the created Array2DT is not used anymore the Dispose method should be called to return the possibly ArrayPool-allocated underlying buffer to the pool.
Public methodArray2DT(ArraySectionT, Int32, Int32)
Initializes a new instance of the Array2DT struct from an existing ArraySectionT using the specified height and width. No heap allocation occurs when using this constructor overload.
Top
Properties

  NameDescription
Public propertyAsMemory
Returns the current Array2DT instance as a Memory instance.
Public propertyAsSpan
Returns the current Array2DT instance as a Span instance.
Public propertyBuffer
Public propertyHeight
Gets the height of this Array2DT instance.
Public propertyIsNull
Gets whether this Array2DT instance represents a  array.
Please note that the ToArray/To2DArray/ToJaggedArray methods return  when this property returns .
Public propertyIsNullOrEmpty
Gets whether this Array2DT instance represents an empty or a  array.
Public propertyItemIndex
Gets a row of the Array2DT as an ArraySectionT instance.
Public propertyItemInt32
Gets a row of the Array2DT as an ArraySectionT instance.
Public propertyItemRange
Gets a new Array2DT instance, which represents a subrange of rows of the current instance indicated by the specified range.
Public propertyItemInt32, Int32
Gets or sets the element at the specified coordinates. Parameter order is the same as in case of a regular two-dimensional array.
To return a reference to an element use the GetElementReference method instead.
Public propertyLength
Gets the total length of this Array2DT instance.
Public propertyWidth
Gets the width of this Array2DT instance.
Top
Methods

  NameDescription
Public methodDispose
Releases the underlying buffer. If this Array2DT instance was instantiated by the self allocating constructor, then this method must be called when the Array2DT is not used anymore. On platforms that do not support the ArrayPool class this method simply clears the self instance.
Public methodEquals(Object)
Determines whether the specified object is equal to this instance.
(Overrides ValueTypeEquals(Object).)
Public methodEquals(Array2DT)
Indicates whether the current Array2DT instance is equal to another one specified in the other parameter.
Public methodGetElementReference
Gets the reference to the element at the specified indices. Parameter order is the same as in case of a regular two-dimensional array.
Public methodGetEnumerator
Returns an enumerator that iterates through the items of this Array2DT.
Public methodGetHashCode
Returns a hash code for this Array2DT instance.
(Overrides ValueTypeGetHashCode.)
Public methodGetPinnableReference
Returns a reference to the first element in this Array2DT. This makes possible to use the Array2DT in a fixed statement.
Public methodSlice(Int32)
Gets a new Array2DT instance, which represents a subrange of rows of the current instance starting with the specified startRowIndex.
Public methodSlice(Int32, Int32)
Gets a new Array2DT instance, which represents a subrange of rows of the current instance indicated by the specified startRowIndex and rowCount.
Public methodTo2DArray
Copies the elements of this Array2DT to a new two dimensional array.
Public methodToArray
Copies the elements of this Array2DT to a new single dimensional array.
Public methodToJaggedArray
Copies the elements of this Array2DT to a new jagged array.
Top
Operators

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

  NameDescription
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 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 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

In .NET Core 3.0/.NET Standard 2.1 and above an Array2DT instance can be easily turned to a Span instance (either by cast or by the AsSpan property).

If the current platform supports it, the underlying array might be obtained by using the ArrayPool.

Note Note
Unlike the underlying ArraySectionT, the Array2DT implements the IDisposable interface. Calling the Dispose method is required if the Array2DT was not created from an existing ArraySectionT instance. Not calling the Dispose method may lead to decreased application performance.

As Array2DT 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