Back to Devexpress

Implement IComparable

coderushforroslyn-115532-coding-assistance-code-providers-implementation-providers-implement-icomparable.md

latest4.0 KB
Original Source

Implement IComparable

  • Mar 03, 2021
  • 3 minutes to read

Purpose

Use this Code Provider to add the implementation of the IComparable interface to any type (for example, class and record) containing at least one field or property. This Code Provider adds the IComparable and IComparable<T> implementations along with the CompareTo methods to the type. The CompareTo method bodies are generated automatically.

Availability

Available when the caret is on a type declaration.

Usage

  1. Place the caret on a type declaration. The type must contain at least one field or property.

  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

  3. Select Implement IComparable from the menu.

After execution, the Code Provider adds the implementation of the IComparable interface and type-specific IComparable<T> interface. It also adds the corresponding CompareTo methods with their common implementation.

csharp
public class Temperature : IComparable, IComparable<Temperature> {
    protected double temperatureF;
    public double Fahrenheit {
        get {
            return this.temperatureF;
        }
        set {
            this.temperatureF = value;
        }
    }
    public double Celsius {
        get {
            return (this.temperatureF - 32) * (5.0 / 9);
        }
        set {
            this.temperatureF = (value * 9.0 / 5) + 32;
        }
    }

    public int CompareTo(object obj) {
        if (obj == null)
            return 1;
        Temperature other = obj as Temperature;
        if (other == null)
            throw new ArgumentException("obj is not a Temperature");
        return CompareTo(other);
    }

    public int CompareTo(Temperature other) {
        if (other == null)
            return 1;
        int result = 0;
        result = this.temperatureF.CompareTo(other.temperatureF);
        if (result != 0)
            return result;
        return result;
    }
}
vb
Public Class Temperature
    Implements IComparable, IComparable(Of Temperature)

    Protected temperatureF As Double
    Public Property Fahrenheit() As Double
        Get
            Return Me.temperatureF
        End Get
        Set(ByVal value As Double)
            Me.temperatureF = value
        End Set
    End Property
    Public Property Celsius() As Double
        Get
            Return (Me.temperatureF - 32) * (5.0 \ 9)
        End Get
        Set(ByVal value As Double)
            Me.temperatureF = (value * 9.0 / 5) + 32
        End Set
    End Property

    Public Function CompareTo(obj As Object) As Integer _
        Implements IComparable.CompareTo
        If obj Is Nothing Then
            Return 1
        End If

        Dim other As Temperature = IIf(TypeOf obj Is Temperature,
                                       CType(obj, Temperature), Nothing)
        If other Is Nothing Then
            Throw New ArgumentException("obj is not a Temperature")
        End If

        Return CompareTo(other)
    End Function

    Public Function CompareTo(other As Temperature) As Integer _
        Implements IComparable(Of Temperature).CompareTo
        If other Is Nothing Then
            Return 1
        End If

        Dim result As Integer = 0
        result = Me.temperatureF.CompareTo(other.temperatureF)
        If result <> 0 Then
            Return result
        End If

        Return result
    End Function
End Class

See Also

Implement ISerializable

Implement IEquatable