JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C#


CSHARP.IDEF.NOEQUALS : Missing Equals Override (C#)

Summary

Equals() is inherited but extra fields have been added.

Redefinitions of the Equals()/GetHashCode() methods from System.Object must be consistent in the sense that, for instance, if two objects are Equals() then GetHashCode() must yield the same value on both. The validity of such a property is in general undecidable, but most incorrect definitions amount to simple cases, where one of the two methods is missing. However, it is often correct to redefine only one of those methods and the analyzer is aware of many such situations.

Inconsistent definitions of Equals()/GetHashCode() induce unexpected behaviors when objects are put inside most collection classes of the standard .NET library.

Properties

Class Name Missing Equals Override (C#)
Significance reliability
Mnemonic CSHARP.IDEF.NOEQUALS
Categories
CWE CWE:1023 Incomplete Comparison with Missing Factors
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="Missing Equals Override (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

Make Equals() consistent with GetHashCode(). In many cases, this just amounts to provide the missing definition for one of them.

Relevant Configuration File Parameters

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

 

To report problems with this documentation, please visit https://support.codesecure.com/.