Runtime: Rename string-like Span.Equals method to SequenceEqual

Created on 20 Mar 2018  路  2Comments  路  Source: dotnet/runtime

We have the following string-like extension method for ReadOnlySpan<char>:
```C#
public static bool Equals(this ReadOnlySpan span, ReadOnlySpan other, StringComparison comparisonType)

However, due to its name, we are unable to leverage the implicit cast from string to `ReadOnlySpan<char>`. The following code throws a compiler error since it fails to match the correct overload.
```C#
string test = "Hello World!";
ReadOnlySpan<char> span = new char[100];
bool result = span.Equals(test, StringComparison.Ordinal);
// Current workarounds - specify the type explicitly or call AsSpan:
// bool result = span.Equals((ReadOnlySpan<char>)test, StringComparison.Ordinal);
// bool result = span.Equals(test.AsSpan(), StringComparison.Ordinal);
// bool result = MemoryExtensions.Equals(span, test, StringComparison.Ordinal);

Error CS0176 Member 'object.Equals(object, object)' cannot be accessed with an instance reference; qualify it with a type name instead

Should we rename the method to SequenceEqual instead?

We already have a SequenceEqual method (which only takes a single argument), which means, we would end up with a non-generic overload, specific to ReadOnlySpan<char>.

C# public static bool SequenceEqual<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> other) where T : System.IEquatable<T> {} public static bool SequenceEqual<T>(this Span<T> span, ReadOnlySpan<T> other) where T : System.IEquatable<T> {} // ADD: public static bool SequenceEqual(this ReadOnlySpan<char> span, ReadOnlySpan<char> other, StringComparison comparisonType) {}

The alternative of adding an Equals overload that takes string as the argument will not work in resolving the overload matching issue.

Other alternatives for names:

  • EqualsTo

cc @KrzysztofCwalina, @terrajobst, @VSadov

api-needs-work area-System.Memory

Most helpful comment

I think living with the cast, span.Equals((ReadOnlySpan)test, StringComparison.Ordinal), is reasonable until 'better betterness' overload resolution is fixed in the next minor update of the C# compiler.

All 2 comments

I think living with the cast, span.Equals((ReadOnlySpan)test, StringComparison.Ordinal), is reasonable until 'better betterness' overload resolution is fixed in the next minor update of the C# compiler.

reasonable until 'better betterness' overload resolution is fixed in the next minor update of the C# compiler

I agree. I had forgotten about the changes to the overload resolution as part of the next minor update.

I just verified that it will work as expected in the future. Closing.

Was this page helpful?
0 / 5 - 0 ratings