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 |
CompareTo() is defined in a class that is not an instance of System.IComparable.
A class C that implements the raw interface System.IComparable must implement the CompareTo(Object) method. If C is made to implement the non-raw interface System.IComparable<C>, then it must implement the CompareTo(C) method instead.
Moreover, it is good practice to make CompareTo() consistent with Equals(): if the comparison of two objects yields 0, then they should be equal. The validity of this implication is in general undecidable. There are, however, frequent situations when, typically, this implication does not hold. An example is when Equals() is inherited from System.Object.
This checker verifies that CompareTo(Object) is defined for classes implementing the raw System.IComparable interface, instead of the (possibly more logical) method CompareTo(C). Moreover, it verifies the consistency of CompareTo() wrt Equals().
Inconsistent definitions of CompareTo()/Equals() induce unexpected behaviors when objects are put inside most sorted collection classes of the standard .NET library.
| Class Name | compareTo in Non-Comparable Class (C#) | |||
|---|---|---|---|---|
| Significance | reliability | |||
| Mnemonic | CSHARP.COMPARE.CTO.NONCOMP | |||
| Categories |
|
|||
| 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="compareTo in Non-Comparable Class (C#)" |
public class MyClass
{
public int CompareTo(object obj) // "compareTo in Non-Comparable Class (C#)" warning issued here
{
//...
return 0;
//...
}
//...
}
In this example, the programmer should rewrite the code as follows.
public class MyClass : System.IComparable
{
public int CompareTo(object obj)
{
//...
return 0;
//...
}
//...
}
}
Use the non-raw interface System.IComparable<C>; make Equals() consistent with CompareTo(). In most cases, this just amounts to providing the missing definition of Equals().
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.