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.HCNOEQUALS : Defines hashCode but not equals (C#)

Summary

The Equals() method seems needed.

The .NET documentation for GetHashCode() specifies that it should return the same value for two objects only if the objects are equal according to Equals(). For this reason, if the GetHashCode() method is redefined, the Equals method should also be redefined in order to avoid possible inconsistencies.

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 Defines hashCode but not equals (C#)
Significance reliability
Mnemonic CSHARP.IDEF.HCNOEQUALS
Categories
CWE CWE:581 Object Model Violation: Just One of Equals and Hashcode Defined
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="Defines hashCode but not equals (C#)"

Example

public class MyClass 
{

    static void Main(string[] args) 
    {
    
        MyClass p1 = new MyClass("hello");
        MyClass p2 = new MyClass("hello");
        
        Console.WriteLine("Are hashcode of p1 and hashcode of p2 the same? "+ (p1.GetHashCode() == p2.GetHashCode()));
        Console.WriteLine("Are p1 and p2 equal? "+ p1.Equals(p2));
    }
    
    public string c;
    
    public MyClass(string c) 
    {
        this.c = c;
    }
    
    public override int GetHashCode() // "Defines hashCode but not equals (C#)" warning issued here 
    {
        return c.GetHashCode(); 
    }
}

The programmer should rewrite the code as follow:

public class MyClass 
{

    static void Main(string[] args) 
    {
    
        MyClass p1 = new MyClass("hello");
        MyClass p2 = new MyClass("hello");
        
        Console.WriteLine("Are hashcode of p1 and hashcode of p2 the same? "+ (p1.GetHashCode() == p2.GetHashCode()));
        Console.WriteLine("Are p1 and p2 equal? "+ p1.Equals(p2));
    }
    
    public string c;
    
    public MyClass(string c) 
    {
        this.c = c;
    }
    
    public override int GetHashCode() 
    {
        return c.GetHashCode(); 
    }
    
    public override boolean Equals(Object other) 
    {

        MyClass other = obj as MyClass;
        if (other == null)
            return false;
        else
            return c.Equals(other.c);

    }
}

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/.