/// Returns a 0-length read-only span whose base is the null pointer.
/// </summary>
public static ReadOnlySpan<T> Empty => default(ReadOnlySpan<T>);
+
+ /// <summary>Gets an enumerator for this span.</summary>
+ public Enumerator GetEnumerator() => new Enumerator(this);
+
+ /// <summary>Enumerates the elements of a <see cref="ReadOnlySpan{T}"/>.</summary>
+ public ref struct Enumerator
+ {
+ /// <summary>The span being enumerated.</summary>
+ private readonly ReadOnlySpan<T> _span;
+ /// <summary>The next index to yield.</summary>
+ private int _index;
+
+ /// <summary>Initialize the enumerator.</summary>
+ /// <param name="span">The span to enumerate.</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal Enumerator(ReadOnlySpan<T> span)
+ {
+ _span = span;
+ _index = -1;
+ }
+
+ /// <summary>Advances the enumerator to the next element of the span.</summary>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public bool MoveNext()
+ {
+ int index = _index + 1;
+ if (index < _span.Length)
+ {
+ _index = index;
+ return true;
+ }
+
+ return false;
+ }
+
+ /// <summary>Gets the element at the current position of the enumerator.</summary>
+ public ref readonly T Current
+ {
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ get
+ {
+ // TODO https://github.com/dotnet/coreclr/pull/14727:
+ // Change this to simply be:
+ // get => ref readonly _span[_index];
+ // once ReadOnlySpan<T>'s indexer returns ref readonly.
+
+ if ((uint)_index >= (uint)_span.Length)
+ {
+ ThrowHelper.ThrowIndexOutOfRangeException();
+ }
+
+ return ref Unsafe.Add(ref _span.DangerousGetPinnableReference(), _index);
+ }
+ }
+ }
}
}
/// Returns an empty <see cref="Span{T}"/>
/// </summary>
public static Span<T> Empty => default(Span<T>);
+
+ /// <summary>Gets an enumerator for this span.</summary>
+ public Enumerator GetEnumerator() => new Enumerator(this);
+
+ /// <summary>Enumerates the elements of a <see cref="Span{T}"/>.</summary>
+ public ref struct Enumerator
+ {
+ /// <summary>The span being enumerated.</summary>
+ private readonly Span<T> _span;
+ /// <summary>The next index to yield.</summary>
+ private int _index;
+
+ /// <summary>Initialize the enumerator.</summary>
+ /// <param name="span">The span to enumerate.</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal Enumerator(Span<T> span)
+ {
+ _span = span;
+ _index = -1;
+ }
+
+ /// <summary>Advances the enumerator to the next element of the span.</summary>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public bool MoveNext()
+ {
+ int index = _index + 1;
+ if (index < _span.Length)
+ {
+ _index = index;
+ return true;
+ }
+
+ return false;
+ }
+
+ /// <summary>Gets the element at the current position of the enumerator.</summary>
+ public ref T Current
+ {
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ get => ref _span[_index];
+ }
+ }
}
}