Array2DT Structure

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 or ArraySegmentT. Depending on the used platform it supports ArrayPoolT allocation and casting to SpanT.

Definition

Namespace: KGySoft.Collections
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 8.2.0
C#
[SerializableAttribute]
public struct Array2D<T> : IDisposable, IEquatable<Array2D<T>>, 
	IEnumerable<T>, IEnumerable
Inheritance
Object    ValueType    Array2DT
Implements
IEnumerableT, IEnumerable, IDisposable, IEquatableArray2DT

Type Parameters

T
The type of the elements in the collection.

Remarks

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

The actual underlying single dimensional array can be accessed via the Buffer property that has an UnderlyingArray property.

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

  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.

Due to the Dispose method Array2DT is a non-readonly struct. It is not recommended to use it as a readonly field; otherwise, accessing its members would make the pre-C# 8.0 compilers to create defensive copies, which leads to a slight performance degradation.

Constructors

Array2DT(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 ArrayPoolT-allocated underlying buffer to the pool.
Array2DT(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.

Properties

AsMemory Returns the current Array2DT instance as a MemoryT instance.
AsSpan Returns the current Array2DT instance as a SpanT instance.
Buffer Gets the underlying buffer as a single dimensional ArraySectionT.
Height Gets the height of this Array2DT instance.
IsNull Gets whether this Array2DT instance represents a array.
Please note that the ToArray/To2DArray/ToJaggedArray methods return when this property returns .
IsNullOrEmpty Gets whether this Array2DT instance represents an empty or a array.
ItemIndex Gets a row of the Array2DT as an ArraySectionT instance.
ItemInt32 Gets a row of the Array2DT as an ArraySectionT instance.
ItemRange Gets a new Array2DT instance, which represents a subrange of rows of the current instance indicated by the specified range.
ItemInt32, 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.
Length Gets the total length of this Array2DT instance.
Width Gets the width of this Array2DT instance.

Methods

Dispose 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 ArrayPoolT class this method simply clears the self instance.
Equals(Array2DT) Indicates whether the current Array2DT instance is equal to another one specified in the other parameter.
Equals(Object) Determines whether the specified object is equal to this instance.
(Overrides ValueTypeEquals(Object))
GetElementReference Gets the reference to the element at the specified indices. Parameter order is the same as in case of a regular two-dimensional array.
GetEnumerator Returns an enumerator that iterates through the items of this Array2DT.
GetHashCode Returns a hash code for this Array2DT instance.
(Overrides ValueTypeGetHashCode)
GetPinnableReference Returns a reference to the first element in this Array2DT. This makes possible to use the Array2DT in a fixed statement.
Slice(Int32) Gets a new Array2DT instance, which represents a subrange of rows of the current instance starting with the specified startRowIndex.
Slice(Int32, Int32) Gets a new Array2DT instance, which represents a subrange of rows of the current instance indicated by the specified startRowIndex and rowCount.
To2DArray Copies the elements of this Array2DT to a new two dimensional array.
ToArray Copies the elements of this Array2DT to a new single dimensional array.
ToJaggedArray Copies the elements of this Array2DT to a new jagged array.

Operators

Equality(Array2DT, Array2DT) Determines whether two specified Array2DT instances have the same value.
(Array2DT to ArraySectionT) Performs an implicit conversion from Array2DT to ArraySectionT.
(Array2DT to ArraySegmentT) Performs an implicit conversion from Array2DT to ArraySegmentT.
(Array2DT to SpanT) Performs an implicit conversion from Array2DT to SpanT.
Inequality(Array2DT, Array2DT) Determines whether two specified Array2DT instances have different values.

Extension Methods

Convert 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)
ConvertTTarget Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions)
ForEachT Similarly to the List<T>.ForEach method, processes an action on each element of an enumerable collection.
(Defined by EnumerableExtensions)
GetRandomElementT Gets a random element from the enumerable source using a new FastRandom instance.
(Defined by EnumerableExtensions)
GetRandomElementT Gets a random element from the enumerable source using a specified Random instance.
(Defined by EnumerableExtensions)
In Gets whether item is among the elements of set.
See the Examples section of the generic InT(T, T) overload for an example.
(Defined by ObjectExtensions)
IndexOf Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions)
IndexOf Searches for an element in the source enumeration.
(Defined by EnumerableExtensions)
IndexOfT Searches for an element in the source enumeration.
(Defined by EnumerableExtensions)
IndexOfT Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions)
IsNullOrEmpty Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions)
IsNullOrEmptyT Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions)
JoinT Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions)
JoinT Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions)
ShuffleT Shuffles an enumerable source (randomizes its elements) using a new FastRandom instance.
(Defined by EnumerableExtensions)
ShuffleT Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions)
ShuffleT Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions)
ShuffleT Shuffles an enumerable source (randomizes its elements) using a specified Random instance.
(Defined by EnumerableExtensions)
ToCircularListT Creates a CircularListT from an IEnumerableT.
(Defined by EnumerableExtensions)
ToStringKeyedDictionaryT Creates a StringKeyedDictionaryTValue from an IEnumerableT instance using the specified keySelector delegate and a comparer.
(Defined by EnumerableExtensions)
ToStringKeyedDictionaryT, TValue Creates a StringKeyedDictionaryTValue from an IEnumerableT instance using the specified key and value selector delegates and a comparer.
(Defined by EnumerableExtensions)
TryAdd Tries to add the specified item to the collection.
(Defined by EnumerableExtensions)
TryAddT Tries to add the specified item to the collection.
(Defined by EnumerableExtensions)
TryAddRange Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions)
TryAddRangeT Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions)
TryClear Tries to remove all elements from the collection.
(Defined by EnumerableExtensions)
TryClearT Tries to remove all elements from the collection.
(Defined by EnumerableExtensions)
TryConvert Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvert Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvertTTarget 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)
TryConvertTTarget 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)
TryGetCount Tries to get the number of elements in the source enumeration without enumerating it.
(Defined by EnumerableExtensions)
TryGetCountT Tries to get the number of elements in the source enumeration without enumerating it.
(Defined by EnumerableExtensions)
TryGetElementAt Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions)
TryGetElementAtT Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions)
TryInsert Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions)
TryInsertT Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions)
TryInsertRange Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions)
TryInsertRangeT Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions)
TryRemove Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions)
TryRemoveT Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions)
TryRemoveAt Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions)
TryRemoveAtT Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions)
TryRemoveRange Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions)
TryRemoveRangeT Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions)
TryReplaceRange 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)
TryReplaceRangeT 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)
TrySetElementAt Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions)
TrySetElementAtT Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions)

See Also