1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System.ComponentModel;
6 using System.Diagnostics;
7 using System.Runtime.CompilerServices;
9 using System.Runtime.Versioning;
11 #pragma warning disable 0809 //warning CS0809: Obsolete member 'Span<T>.Equals(object)' overrides non-obsolete member 'object.Equals(object)'
16 /// ReadOnlySpan represents a contiguous region of arbitrary memory. Unlike arrays, it can point to either managed
17 /// or native memory, or to memory allocated on the stack. It is type- and memory-safe.
19 [DebuggerTypeProxy(typeof(SpanDebugView<>))]
20 [DebuggerDisplay("{ToString(),raw}")]
21 public readonly ref partial struct ReadOnlySpan<T>
24 /// The number of items in the read-only span.
36 /// Returns true if Length is 0.
47 /// Returns false if left and right point at the same memory and have the same length. Note that
48 /// this does *not* check to see if the *contents* are equal.
50 public static bool operator !=(ReadOnlySpan<T> left, ReadOnlySpan<T> right) => !(left == right);
53 /// This method is not supported as spans cannot be boxed. To compare two spans, use operator==.
54 /// <exception cref="System.NotSupportedException">
55 /// Always thrown by this method.
58 [Obsolete("Equals() on ReadOnlySpan will always throw an exception. Use == instead.")]
59 [EditorBrowsable(EditorBrowsableState.Never)]
60 public override bool Equals(object obj)
62 throw new NotSupportedException(SR.NotSupported_CannotCallEqualsOnSpan);
66 /// This method is not supported as spans cannot be boxed.
67 /// <exception cref="System.NotSupportedException">
68 /// Always thrown by this method.
71 [Obsolete("GetHashCode() on ReadOnlySpan will always throw an exception.")]
72 [EditorBrowsable(EditorBrowsableState.Never)]
73 public override int GetHashCode()
75 throw new NotSupportedException(SR.NotSupported_CannotCallGetHashCodeOnSpan);
79 /// Defines an implicit conversion of an array to a <see cref="ReadOnlySpan{T}"/>
81 public static implicit operator ReadOnlySpan<T>(T[] array) => new ReadOnlySpan<T>(array);
84 /// Defines an implicit conversion of a <see cref="ArraySegment{T}"/> to a <see cref="ReadOnlySpan{T}"/>
86 public static implicit operator ReadOnlySpan<T>(ArraySegment<T> segment)
87 => new ReadOnlySpan<T>(segment.Array, segment.Offset, segment.Count);
90 /// Returns a 0-length read-only span whose base is the null pointer.
92 public static ReadOnlySpan<T> Empty => default;
94 /// <summary>Gets an enumerator for this span.</summary>
95 public Enumerator GetEnumerator() => new Enumerator(this);
97 /// <summary>Enumerates the elements of a <see cref="ReadOnlySpan{T}"/>.</summary>
98 public ref struct Enumerator
100 /// <summary>The span being enumerated.</summary>
101 private readonly ReadOnlySpan<T> _span;
102 /// <summary>The next index to yield.</summary>
105 /// <summary>Initialize the enumerator.</summary>
106 /// <param name="span">The span to enumerate.</param>
107 [MethodImpl(MethodImplOptions.AggressiveInlining)]
108 internal Enumerator(ReadOnlySpan<T> span)
114 /// <summary>Advances the enumerator to the next element of the span.</summary>
115 [MethodImpl(MethodImplOptions.AggressiveInlining)]
116 public bool MoveNext()
118 int index = _index + 1;
119 if (index < _span.Length)
128 /// <summary>Gets the element at the current position of the enumerator.</summary>
129 public ref readonly T Current
131 [MethodImpl(MethodImplOptions.AggressiveInlining)]
132 get => ref _span[_index];