ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Code inspection: Values of types marked with 'CannotApplyEqualityOperatorAttribute' should be compared using 'Equals()'

Last modified: 08 April 2024

ReSharper provides the [CannotApplyEqualityOperator] attribute in ReSharper.Annotations to mark types for which the use of == and != operators is inappropriate and should be replaced with Equals(). Consequently, when two types marked with this attribute are compared using == or !=, ReSharper will issue a warning.

ReSharper assumes that using the equality operators to compare with null is self-evident and will not issue a warning in this case.

You may also wonder how ReSharper knows which library types cannot be compared with equality operators. Well, the trick is done with external annotations for the .NET Framework Class Library and other frequently used libraries. These annotations are included in the ReSharper installation. The [CannotApplyEqualityOperator] attribute that triggers this inspection can also be used for your custom types, or you can use the mechanism of external annotations to annotate types in other compiled libraries that you use.