JetBrains Rider 2024.3 Help

Code inspection: Possibly wrong string comparison: spans are only equal when pointing to the same memory location

This inspection reports a potential issue with comparing a span to a string using the == operator, which checks for reference equality, not content equality. Such a comparison checks whether the span and the string are pointing to the same memory location, which is not what you usually want.

To compare the contents of the span and the string, you can use the is operator.

bool IsHello(ReadOnlySpan<char> span) { return span == "Hello"; }
bool IsHello(ReadOnlySpan<char> span) { return span is "Hello"; }
Last modified: 06 August 2024