Skip to content
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

Rule: MA0006 Use String.Equals instead of equality operator #24

Closed
kimlundjohansen opened this issue Dec 16, 2020 · 7 comments
Closed
Assignees

Comments

@kimlundjohansen
Copy link

Rule summary

Value
Title Use String.Equals instead of equality operator
CheckId MA0006
Category ?
Link https://github.com/meziantou/Meziantou.Analyzer/blob/master/docs/Rules/MA0006.md

What is the problem

This does not improve readability as the rules description claims.

Suggestion

disable this rule in root .editorconfig with dotnet_diagnostic.MA0006.severity = none

@egil
Copy link
Contributor

egil commented Dec 16, 2020

What StringComparison style is used with a == b? If it is not StringComparison.Ordinal, then a.Equals(b, StringComparison.Ordinal will perform better.

Besides that, I agree that it does NOT improve readability.

@davidkallesen davidkallesen changed the title Rule: MA0006 Rule: MA0006 Use String.Equals instead of equality operator Jan 15, 2021
@cjakobsen
Copy link
Contributor

This documentation from Microsoft also speaks against using == for comparison so I think we should keep it.

@MrAndersPedersen
Copy link

Please red comment from cjacobsen.

@rickykaare
Copy link

rickykaare commented Feb 5, 2021

As far as I understand using == for comparing two strings will be the same as using string.Equals() with StringComparison.Ordinal (given that the two strings are of type string, and not cast to e.g. object).

In my mind, this way of comparing strings are by far the most commonly used, so applying the MA0006 rule in general would add a lot of clutter.

In most cases I think it would be very obvious if you need to compare using case-insensitivity or the CurrentCulture, and then use the string.Equals() instead.

@egil
Copy link
Contributor

egil commented Feb 9, 2021

As far as I understand using == for comparing two strings will be the same as using string.Equals() with StringComparison.Ordinal

That's surprising to me @rickykaare, but you are right, at least if we read the latest string.cs code. Here we see that EqualsHelper is called when from both the operators ==/!= and from the Equals method when StringComparison.Ordinal is set.

That takes the performance concern out of the equation for me. The question that is left is whether we want to be explicit in our code about our intentions, e.g. signaling that, yes, we have thought about culture and this is what we want.

@egil
Copy link
Contributor

egil commented Feb 11, 2021

ROOT=NONE, because in most cases we are only doing ordinal comparisons anyway, and == leads to cleaner code.

Exception to this rule when working on an application that is multi cultured.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants