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.
See the Remarks section for details.
Namespace: KGySoft.Collections
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.5.0-rc.1
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
Name | Description | |
---|---|---|
![]() | 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 ArrayPool-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
Name | Description | |
---|---|---|
![]() | AsMemory |
Returns the current Array2DT instance as a Memory instance.
|
![]() | AsSpan |
Returns the current Array2DT instance as a Span 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
Name | Description | |
---|---|---|
![]() | 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 ArrayPool class this method simply clears the self instance.
|
![]() | Equals(Object) |
Determines whether the specified object is equal to this instance.
(Overrides ValueTypeEquals(Object).) |
![]() | Equals(Array2DT) |
Indicates whether the current Array2DT instance is equal to another one specified in the other parameter.
|
![]() | 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
Name | Description | |
---|---|---|
![]() ![]() | Equality |
Determines whether two specified Array2DT instances have the same value.
|
![]() ![]() | (Array2DT to ArraySectionT) |
Performs an implicit conversion from Array2DT to ArraySectionT.
|
![]() ![]() | (Array2DT to SpanT) |
Performs an implicit conversion from Array2DT to Span<T>.
|
![]() ![]() | Inequality |
Determines whether two specified Array2DT instances have different values.
|
Extension Methods
Name | Description | |
---|---|---|
![]() | Convert(Type, CultureInfo) | Overloaded.
Converts an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example. |
![]() ![]() | ConvertTTarget(CultureInfo) | Overloaded. (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(Boolean) | Overloaded.
Gets a random element from the enumerable source using a new FastRandom instance.
(Defined by EnumerableExtensions.) |
![]() | GetRandomElementT(Random, Boolean) | Overloaded.
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.
(Defined by ObjectExtensions.)See the Examples section of the generic InT(T, T) overload for an example. |
![]() | IndexOf(FuncObject, Boolean) | Overloaded.
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions.) |
![]() | IndexOf(Object) | Overloaded.
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions.) |
![]() | IndexOfT(FuncT, Boolean) | Overloaded.
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions.) |
![]() | IndexOfT(T) | Overloaded.
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions.) |
![]() | IsNullOrEmpty | Overloaded.
Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions.) |
![]() | IsNullOrEmptyT | Overloaded.
Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions.) |
![]() | JoinT(String) | Overloaded.
Concatenates the items of the source collection into a new string instance
using the specified separator between the items.
(Defined by EnumerableExtensions.) |
![]() | JoinT(Char) | Overloaded.
Concatenates the items of the source collection into a new string instance
using the specified separator between the items.
(Defined by EnumerableExtensions.) |
![]() | ShuffleT | Overloaded.
Shuffles an enumerable source (randomizes its elements) using a new FastRandom instance.
(Defined by EnumerableExtensions.) |
![]() | ShuffleT(Int32) | Overloaded.
Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions.) |
![]() | ShuffleT(Random) | Overloaded.
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.) |
![]() | TryAdd(Object, Boolean, Boolean) | Overloaded.
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryAddT(T, Boolean, Boolean) | Overloaded.
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryAddRange(IEnumerable, Boolean, Boolean) | Overloaded.
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions.) |
![]() | TryAddRangeT(IEnumerableT, Boolean, Boolean) | Overloaded.
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions.) |
![]() | TryClear(Boolean, Boolean) | Overloaded.
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions.) |
![]() | TryClearT(Boolean, Boolean) | Overloaded.
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions.) |
![]() | TryConvert(Type, Object) | Overloaded. (Defined by ObjectExtensions.) |
![]() | TryConvert(Type, CultureInfo, Object) | Overloaded. (Defined by ObjectExtensions.) |
![]() | TryConvertTTarget(TTarget) | Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. |
![]() | TryConvertTTarget(CultureInfo, TTarget) | Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. |
![]() | TryGetElementAt(Int32, Object, Boolean, Boolean) | Overloaded.
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions.) |
![]() | TryGetElementAtT(Int32, T, Boolean, Boolean) | Overloaded.
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions.) |
![]() | TryInsert(Int32, Object, Boolean, Boolean) | Overloaded.
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryInsertT(Int32, T, Boolean, Boolean) | Overloaded.
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryInsertRange(Int32, IEnumerable, Boolean, Boolean) | Overloaded.
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions.) |
![]() | TryInsertRangeT(Int32, IEnumerableT, Boolean, Boolean) | Overloaded.
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions.) |
![]() | TryRemove(Object, Boolean, Boolean) | Overloaded.
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryRemoveT(T, Boolean, Boolean) | Overloaded.
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryRemoveAt(Int32, Boolean, Boolean) | Overloaded.
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions.) |
![]() | TryRemoveAtT(Int32, Boolean, Boolean) | Overloaded.
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions.) |
![]() | TryRemoveRange(Int32, Int32, Boolean, Boolean) | Overloaded.
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions.) |
![]() | TryRemoveRangeT(Int32, Int32, Boolean, Boolean) | Overloaded.
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions.) |
![]() | TryReplaceRange(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.) |
![]() | TryReplaceRangeT(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.) |
![]() | TrySetElementAt(Int32, Object, Boolean, Boolean) | Overloaded.
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions.) |
![]() | TrySetElementAtT(Int32, T, Boolean, Boolean) | Overloaded.
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions.) |
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.
![]() |
---|
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