The ReadOnlyMemory<T>
and ReadOnlySpan<T>
provide Equal(other)
method but no CompareTo(other)
. I wonder if there a more efficient way to implement IComparable<ReadOnlyMemory<T>>
other than a loop and compare each element. In my case T
is either char
or byte
.
There does not appear to be a native method of comparing ReadOnlyMemory instances. There are however native methods for ReadOnlySpan, which can be accessed by using the ReadOnlyMemory's .Span property:
ReadOnlyMemory<T> One = ...;
ReadOnlyMemory<T> Two = ...;
bool ContentsEqual = One.Span.SequenceEqual(Two.Span);
int Comparison = One.Span.SequenceCompareTo(Two.Span);
Enjoy :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With