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.CTOEQ : compareTo/equals mismatch (C#)

要旨

CompareTo() is defined is defined inconsistently from Equals().

By convention the CompareTo should return 0 if two instances are equal, and it should return 1 or -1 only if the instances are different in order to preserve the total ordering.

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.

プロパティ

クラス名 compareTo/equals mismatch (C#)
日本語クラス名 compareTo/equals mismatch (C#)
クラス分類 信頼性 (reliability)
ニーモニック CSHARP.IDEF.CTOEQ
カテゴリー
CWE CWE:697 Incorrect Comparison
対応言語 C# で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="compareTo/equals mismatch (C#)"

using System;

namespace DocumentationExamples
{

    public abstract class CompareTo : IComparable<CompareTo>
    {
        private static int nextId;
        private readonly int id = nextId++;
        int IComparable<CompareTo>.CompareTo(CompareTo other)               // compareTo without equals (C#) warning issued here
        {
            return id - other.id;
        }
    }
    public class CompareToA : CompareTo
    {
        public static void Main(string[] args)
        { }

        private readonly string name;
        public CompareToA(string name)
        {
            this.name = name;
        }
    }
    public sealed class CompareToVsEquals : IComparable<CompareToVsEquals>
    {
        private readonly int f;
        public CompareToVsEquals(int f)
        {
            this.f = f;
        }
        int IComparable<CompareToVsEquals>.CompareTo(CompareToVsEquals o)   // Two warnings issued here: 
                                                                            // - compareTo without equals (C#)
                                                                            // - Asymmetric compareTo (C#)
        {
            if (f > 0)
                return -1;
            else
                return 0;
        }
        public override string ToString()
        {
            return f.ToString();
        }
    }
    public sealed class CompareToVsEquals2 : IComparable<CompareToVsEquals2>
    {
        private int f;
        public CompareToVsEquals2(int f)
        {
            this.f = f;
        }
        int IComparable<CompareToVsEquals2>.CompareTo(CompareToVsEquals2 o) // compareTo/equals mismatch (C#) warning issued here
        {
            if (this == o || Equals(o))
                return 1;
            else
                return -1;
        }
        public override bool Equals(object other)
        {
            return other is CompareToVsEquals2 && ((CompareToVsEquals2)other).f == f;
        }
        public override string ToString()
        {
            return f.ToString();
        }
        public override int GetHashCode()
        {
            return f.GetHashCode();
        }
    }
}

解決法

Respect .Net convention, the CompareTo should return 0 if two instances are equal, and it should return 1 or -1 only if the instances are different in order to preserve the total ordering.

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。

 

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