CastArrayTFrom, TTo Structure

Represents a one dimensional array (or a section of it) where the original element type of TFrom is cast to another element type of TTo. Can be helpful on platforms where SpanT is not available or wherever you must use arrays that you want to reinterpret. For example, if you want to retrieve only byte arrays from the ArrayPoolT but you want to reinterpret them as other array types.

Definition

Namespace: KGySoft.Collections
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 9.0.0
C#
[SerializableAttribute]
public readonly struct CastArray<TFrom, TTo> : IList<TTo>, 
	ICollection<TTo>, IEnumerable<TTo>, IEnumerable, IList, ICollection, 
	IEquatable<CastArray<TFrom, TTo>>, IReadOnlyList<TTo>, IReadOnlyCollection<TTo>
where TFrom : struct, new(), Object
where TTo : struct, new(), Object
Inheritance
Object    ValueType    CastArrayTFrom, TTo
Implements
ICollectionTTo, IEnumerableTTo, IListTTo, IReadOnlyCollectionTTo, IReadOnlyListTTo, ICollection, IEnumerable, IList, IEquatableCastArrayTFrom, TTo

Type Parameters

TFrom
The actual element type of the underlying array.
TTo
The reinterpreted element type of the underlying array.

Remarks

CastArrayTFrom, TTo is very similar to a reinterpreted SpanT, but as the underlying buffer can only be an array, it can reside on the heap, too (just like MemoryT). Actually on platforms where SpanT and MemoryT are available you can use the AsSpan and AsMemory properties.

Unlike ArraySectionT, CastArrayTFrom, TTo has no self-allocating constructors. But you can pass an ArraySectionT instance to the constructor that allocated a buffer by itself. In such case it's the caller's responsibility to call the Release method in the end to return the possibly rented array to the pool.

Though this type also supports slicing, it requires that the address of the start element (in terms of TTo) must be aligned to an element in the underlying array of TFrom elements. It's not an issue for any start index if the size of TFrom is 1 or equal to the size of TTo. Otherwise, the Slice methods may throw an ArgumentException.

  Caution

.NET Framework 4.x only: When this type is used in a partially trusted domain that does not allow executing unverifiable code, then the indexers and some methods may throw a NotSupportedException. You can avoid it by enabling the SkipVerification flag at the security permissions when creating the AppDomain.

  Tip

See more details and some examples about KGy SOFT's span-like types at the Remarks section of the ArraySectionT type.

Constructors

CastArrayTFrom, TTo Initializes a new instance of the CastArrayTFrom, TTo struct from the specified ArraySectionT.

Properties

AsMemory Returns this CastArrayTFrom, TTo as a MemoryT instance. Please note that getting this property allocates a custom MemoryManagerT instance on the heap internally.
AsSpan Returns this CastArrayTFrom, TTo as a SpanT instance.
Buffer Gets the underlying buffer of this CastArrayTFrom, TTo as an ArraySectionT instance.
IsNull Gets whether this CastArrayTFrom, TTo instance represents a array.
Please note that the ToArray method returns when this property returns .
IsNullOrEmpty Gets whether this CastArrayTFrom, TTo instance represents an empty array section or a array.
Item Gets or sets the element at the specified index.
Length Gets the number of TTo elements in this CastArrayTFrom, TTo. To get the number of TFrom elements check the Buffer property.

Methods

As2D Gets this CastArrayTFrom, TTo as an CastArray2DTFrom, TTo instance using the specified height and width. The CastArrayTFrom, TTo must have enough capacity for the specified dimensions.
As3D Gets this CastArrayTFrom, TTo as an CastArray2DTFrom, TTo instance using the specified height and width. The CastArrayTFrom, TTo must have enough capacity for the specified dimensions.
CastT Reinterprets the TTo type of this CastArrayTFrom, TTo to type T.
Cast2DT Reinterprets this CastArrayTFrom, TTo as a two-dimensional CastArray2DTFrom, TTo struct, casting the TTo type of this CastArrayTFrom, TTo to type T.
Cast3DT Reinterprets this CastArrayTFrom, TTo as a three-dimensional CastArray3DTFrom, TTo struct, casting the TTo type of this CastArrayTFrom, TTo to type T.
Clear Clears the items in this CastArrayTFrom, TTo instance so all elements will have the default value of type TTo. To clear the items with a specific value use the Fill method instead.
Contains Determines whether this CastArrayTFrom, TTo contains the specific item.
CopyTo(ArraySectionTTo) Copies the items of this CastArrayTFrom, TTo to a compatible ArraySectionT.
CopyTo(CastArrayTFrom, TTo) Copies the items of this CastArrayTFrom, TTo to a compatible instance.
CopyTo(TTo, Int32) Copies the items of this CastArrayTFrom, TTo to a compatible one-dimensional array, starting at a particular index.
Equals(CastArrayTFrom, TTo) Indicates whether the current CastArrayTFrom, TTo instance is equal to another one specified in the other parameter. That is, when they have the same Length, and they both reference the same section of the same underlying array.
Equals(Object) Determines whether the specified object is equal to this instance.
(Overrides ValueTypeEquals(Object))
Fill Assigns the specified value to all elements in this CastArrayTFrom, TTo instance.
GetElementReference Gets the reference to the element at the specified index.
GetElementReferenceUnsafe Gets the reference to the element at the specified index without any range check or validation. This method can even throw a NullReferenceException if the IsNull property returns . To validate index against Length use the GetElementReference method instead.
GetElementUnsafe Gets the element at the specified index without any range check or validation. This method can even throw a NullReferenceException if the IsNull property returns . To validate index against Length use the indexer instead.
GetEnumerator Returns an enumerator that iterates through the items of this CastArrayTFrom, TTo.
GetHashCode Returns a hash code for this CastArrayTFrom, TTo instance.
(Overrides ValueTypeGetHashCode)
GetPinnableReference Returns a reference to the first element in this CastArrayTFrom, TTo. This makes possible to use the CastArrayTFrom, TTo in a fixed statement.
IndexOf Determines the index of a specific item in this CastArrayTFrom, TTo.
SetElementUnsafe Sets the element at the specified index without any range check or validation. This method can even throw a NullReferenceException if the IsNull property returns . To validate index against Length use the indexer instead.
Slice(Int32) Gets a new CastArrayTFrom, TTo instance, which represents a subsection of the current instance with the specified startIndex. Please note that the size of TTo multiplied by startIndex must be divisible by the size of TFrom.
Slice(Int32, Int32) Gets a new CastArrayTFrom, TTo instance, which represents a subsection of the current instance with the specified startIndex and length. Please note that the size of TTo multiplied by startIndex must be divisible by the size of TFrom.
ToArray Copies the elements of this CastArrayTFrom, TTo to a new array of element type TTo.

Operators

Equality(CastArrayTFrom, TTo, CastArrayTFrom, TTo) Determines whether two specified CastArrayTFrom, TTo instances have the same value.
(ArraySectionTFrom to CastArrayTFrom, TTo) Performs an implicit conversion from ArraySectionT to CastArrayTFrom, TTo.
(ArraySegmentTFrom to CastArrayTFrom, TTo) Performs an implicit conversion from ArraySegmentT to CastArrayTFrom, TTo.
(TFrom to CastArrayTFrom, TTo) Performs an implicit conversion from array of TFrom to CastArrayTFrom, TTo.
(CastArrayTFrom, TTo to SpanTTo) Performs an implicit conversion from CastArrayTFrom, TTo to a SpanT of element type TTo.
Inequality(CastArrayTFrom, TTo, CastArrayTFrom, TTo) Determines whether two specified CastArrayTFrom, TTo instances have different values.

Fields

Empty Represents the empty CastArrayTFrom, TTo. This field is read-only.
Null Represents the  CastArrayTFrom, TTo. This field is read-only.

Extension Methods

AddRangeTTo Adds a collection to the target ICollectionT.
(Defined by CollectionExtensions)
AsThreadSafeTTo Returns a LockingListT, which provides a thread-safe wrapper for the specified list. This only means that if the members are accessed through the returned LockingListT, then the inner state of the wrapped list remains always consistent and not that all the multi-threading concerns can be ignored.
See the Remarks section of the LockingListT class for details and some examples.
(Defined by ListExtensions)
AsThreadSafeTTo Returns a LockingCollectionT, which provides a thread-safe wrapper for the specified collection. This only means that if the members are accessed through the returned LockingCollectionT, then the inner state of the wrapped collection remains always consistent and not that all the multi-threading concerns can be ignored.
See the Remarks section of the LockingCollectionT class for details and some examples.
(Defined by CollectionExtensions)
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)
ForEachTTo Similarly to the List<T>.ForEach method, processes an action on each element of an enumerable collection.
(Defined by EnumerableExtensions)
GetRandomElementTTo Gets a random element from the enumerable source using a new FastRandom instance.
(Defined by EnumerableExtensions)
GetRandomElementTTo 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)
IndexOfTTo Searches for an element in the source enumeration.
(Defined by EnumerableExtensions)
IndexOfTTo Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions)
InsertRangeTTo Inserts a collection into the target IListT.
(Defined by ListExtensions)
IsNullOrEmpty Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions)
IsNullOrEmptyTTo Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions)
JoinTTo Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions)
JoinTTo Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions)
RemoveRangeTTo Removes count amount of items from the specified collection at the specified index.
(Defined by ListExtensions)
ReplaceRangeTTo 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)
ShuffleTTo Shuffles an enumerable source (randomizes its elements) using a new FastRandom instance.
(Defined by EnumerableExtensions)
ShuffleTTo Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions)
ShuffleTTo Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions)
ShuffleTTo Shuffles an enumerable source (randomizes its elements) using a specified Random instance.
(Defined by EnumerableExtensions)
ToCircularListTTo Creates a CircularListT from an IEnumerableT.
(Defined by EnumerableExtensions)
ToStringKeyedDictionaryTTo Creates a StringKeyedDictionaryTValue from an IEnumerableT instance using the specified keySelector delegate and a comparer.
(Defined by EnumerableExtensions)
ToStringKeyedDictionaryTTo, 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)
TryAddTTo 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)
TryAddRangeTTo 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)
TryClearTTo 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)
TryGetCountTTo 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)
TryGetElementAtTTo 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)
TryInsertTTo 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)
TryInsertRangeTTo 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)
TryRemoveTTo 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)
TryRemoveAtTTo 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)
TryRemoveRangeTTo 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)
TryReplaceRangeTTo 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)
TrySetElementAtTTo Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions)

See Also