-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prefer string.Equals over string.Compare(string, string) == 0 or != 0 #34514
Comments
@terrajobst to consider for inclusion in the Roslyn Analyzers project. |
cc @GrabYourPitchforks who I think has commented on this before. |
The thing that messes people up is that if you don't specify an explicit comparison, IMO this particular issue should be performance rather than correctness. Let existing issue dotnet/roslyn-analyzers#2581 (comment) be the "correctness / security" issue. |
Shouldn't we keep this issue open? There is some benefit to having devs prefer |
Oh, I misunderstood. I thought you were suggesting we close it in favor of the other issue. But now I see that you simply suggested I revise the category. |
There are a few hits in dotnet/runtime itself: cc @stephentoub |
We have https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2251. This issue should have been closed with that. Regarding "There are a few hits in dotnet/runtime itself", those are all for |
As a carryover from native code habits, I see folks use
string.Compare(a, b) == 0
or!= 0
a lot wherestring.Equals(a, b)
is more semantically meaningful.But more importantly, Compare is for sorting strings whereas Equals is for determining equality. In particular cultures two strings may have sort equivalence without being equal. So in some cases it may even make a security difference (when StringComparison isn't specified).
Category: Performance
The text was updated successfully, but these errors were encountered: