C#


CSHARP.COMPARE.EQ : Should Use equals() Instead of == (C#)

Summary

Two objects are compared with == but Equals() seems more appropriate instead.

This checker identifies incorrect or inefficient comparisons through Equals() or ==. In some cases, these comparisons are wrong: for instance, strings should be compared for equality through Equals() rather than through ==. Classes can be safely compared through == instead. In other cases, these comparisons can be replaced by more efficient code.

Properties

Class Name Should Use equals() Instead of == (C#)
Significance reliability
Mnemonic CSHARP.COMPARE.EQ
Categories
CWE CWE:595 Comparison of Object References Instead of Object Contents
Availability Available for C# only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Should Use equals() Instead of == (C#)"

Example

using System;
using System.Diagnostics;

namespace DocumentationExamples
{

    public class BadEq
    {
        public static bool verbose;
        private readonly static string[] arr = new string[] { "verbose" };
        public static void Main(string[] args)
        {
            if (args.Length > 0)
                Debug.Assert(!args[0].Equals(""));   // Comparison to Empty String (C#) warning issued here
            if (args.Length > 0 && args[0] == "verbose")
                verbose = true;
            if (args.Equals(arr))                    // equals on Array (C#) warning issued here
                verbose = true;
            if (args.Length > 0)
            {
                BadEq m = new BadEq();
                Console.WriteLine(m.Test(args[0]));
                Console.WriteLine(m.WeAreRedefined());
            }
        }
        private bool Test(object o)
        {
            return o == this;                        // == Always Fails (C#)  warning issued here
                                                     // - operands have incompatible types
        }
        public bool WeAreRedefined()
        {
            return !GetType().Equals(typeof(BadEq)); // Missing Equals Override (C#) warning issued here
        }
        
        public bool EqualityTest()
        {
            object name = "myname";
            char[] values = { 'm', 'y', 'n', 'a', 'm', 'e' };
            object myName = new string(values);
            return name == myName;                   // Should Use equals() Instead of == (C#) warning issued here
        }
    }
}

In this example, the programmer should rewrite the code as follows:

public static bool verbose;
private readonly static string[] arr = new string[] { "verbose" };
public static void Main(string[] args)
{
    if (args.Length > 0)
        Debug.Assert(!string.IsNullOrEmpty(args[0]));
    if (args.Length > 0 && args[0].Equals("verbose"))
        verbose = true;
    if (args.SequenceEqual(arr))
        verbose = true;
    if (args.Length > 0)
    {
        BadEq m = new BadEq();
        //Console.WriteLine(m.Test(args[0]));
        Console.WriteLine(m.WeAreRedefined());
    }
}

public bool WeAreRedefined()
{
    return GetType() != typeof(BadEq);
}

Resolution

Check if the equality is actually wrong or can be replaced by more optimized code.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.