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.
Syntax:
public struct Span<T>.Enumerator
Parameter:
Here the parameter is T, where T is the type of items in the Span<T>.
Example:
csharp
using System;
using System.Threading.Tasks;
class GFG {
private static byte[] arr = new byte[4];
static void Main()
{
new Random(30).NextBytes(arr);
Span<byte> sp = arr;
Task.Run(() => clear());
print(sp);
}
public static void clear()
{
Task.Delay(10).Wait();
lock(arr)
{
Array.Clear(arr, 0, arr.Length);
}
}
public static void print(Span<byte> span)
{
foreach(byte e in span)
{
Console.WriteLine(e);
Task.Delay(10).Wait();
}
}
}
|
Output:
52
239
0
0

Properties:
- Current: It is used to get a reference to the item at the current position of the enumerator.
Methods:
- MoveNext(): It advances the enumerator to the next item of the Span<T>.
Important Points:
- 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.
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
23 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...