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.
- Foe 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.
- C# | ValueTuple <T1,T2,T3,T4,T5,T6,T7> Struct
- C# | SByte Struct Fields
- C# | Char Struct
- Byte Struct in C#
- Decimal Struct in C#
- C# | Int16 Struct
- C# | Int32 Struct
- C# | Int 64 Struct
- C# | UInt16 Struct
- C# | UInt32 Struct
- C# | UInt64 Struct
- ValueTuple Struct in C#
- C# | ValueTuple <T1> Struct
- C# | ValueTuple <T1,T2> Struct
- C# | ValueTuple <T1,T2,T3> Struct
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai