Span<T>.Enumerator Struct in C#
Span<T> is a new feature introduced with C# 7.2 and supported in the .NET Core 2.1. It provides a type-safe access to a contiguous area of memory. This memory can be located on the heap, stack or formed a unallocated memory. Span<T> is defined as a ref struct, which means it is limited to being allocated only on the Stack. The main reason for the ref struct design is to ensure that when using of Span<T> it causes no additional heap allocations. Span<T> improves execution speed and reducing memory allocations in existing code.
Here, Span<T> references some contiguous memory(colored blocks) that has already been allocated.
Span<T>.Enumerator struct provides an enumerator for the elements of a Span<T>. Where Span<T> provides a type and memory safe representation of a contiguous region of arbitrary memory.
public struct Span<T>.Enumerator
Here the parameter is T, where T is the type of items in the Span<T>.
52 239 0 0
- Current: It is used to get a reference to the item at the current position of the enumerator.
- MoveNext(): It advances the enumerator to the next item of the Span<T>.
- Span<T> provides a type-safe access to a contiguous area of memory.
- It cannot be used within asynchronous methods.
- It does not implement the IEnumerator or IEnumerator<T> interface, because Span<T> enumerator is a ref struct.
- It does not include a Reset method.
- For use the Reset() method, it must be implemented as part of the interface.
- It does not include additional heap allocation.
- It cannot be used as a generic type argument.
- Span<T> provides read-write access to the memory.
- ReadOnlySpan<T> provides read-only access to a memory.