Visual C#: Can I call the CompareTo method required by the IComparable interface?

Mark Green 41 Reputation points
2021-04-14T23:00:34.13+00:00

Can I call the CompareTo() method required by the IComparable interface or is it only available to the System? In the following code, I can call the .Sort() method which uses the Icomparable<T>.CompareTo method that I wrote but I cannot call the .CompareTo method. Do I just have the wrong syntax?

When I try to type in the line to call .CompareTo, Visual Studio shows a red, squiggly line under CompareTo with this message:
"CS1061: 'Program.Person' does not contain a definition for 'CompareTo' and no accessible extension method 'CompareTo' accepting a first argument of type 'Program.Person' could be found"

class Program
{
    public class Person : IComparable<Person>
    {
        public string sName;
        public int iAge;

        public Person(string name, int age)
        {
            this.sName = name;
            this.iAge = age;
        }

        int IComparable<Person>.CompareTo(Person otherPerson)
        {
            // check if other person is a valid object reference
            if (otherPerson == null)
            {
                if (this == null) return 0;     // if both persons are null, return EQUAL
                return 1;                       // if only other person is null, this person is greater
            }

            if (this == null) return -1;        // if only this person is null, it is lesser

           if (this.sName != otherPerson.sName) return String.Compare(this.sName, otherPerson.sName);
            return this.iAge - otherPerson.iAge;
        }
    }

    static void Main(string[] args)
    {
        List<Person> People = new List<Person>();

        People.Add(new Person("Joe", 25));
        People.Add(new Person("Clark", 36));
        People.Add(new Person("Lois", 33));

        // I can call the .Sort() method so the System can access my .CompareTo() method
        People.Sort();

        // Visual Studio does not accept this line. Error CS1061
        if(People[0].CompareTo(People[1]) < 0)
        {
            // do something
        }

        Person Person1 = new Person("Bill", 28);
        Person Person2 = new Person("Becky", 24);

        // Visual Studio does not accept this line either. Error CS1061
        if (Person1.CompareTo(Person2) < 0)
        {
            // do something
        }
    }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,204 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 111.8K Reputation points
    2021-04-15T13:28:02.597+00:00

    If you change the definition to:

    public int CompareTo( Person otherPerson )
    {
       . . .
    }
    

    then the errors will disappear.

    If you do not want such change, keeping “explicit interface implementation”, then you have to write something like this:

    if( ( (IComparable<Person>)People[0] ).CompareTo( People[1] ) < 0 )
    {
       // do something
    }
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,031 Reputation points
    2021-04-15T00:39:29.99+00:00

    Have you read the docs and looked at the samples?

    Microsoft Learn, simple code sample.

    0 comments No comments

  2. Mark Green 41 Reputation points
    2021-04-15T15:44:34.457+00:00

    Thank you very much for your replies.

    karenpayneoregon, yes I searched at length and looked at many examples but could not find how to call the CompareTo() method myself as opposed to the system using it for .Sort(). However, my problem was not how I called the CompareTo() method. It was how I declared it.

    Resources I looked at showed that I should use this syntax to satisfy the requirement by the IComparable interface to provide a CompareTo() method:

    int IComparable<Person>.CompareTo(Person otherPerson)

    Based on your responses, I think this is incorrect and that the following syntax will satisfy the IComparable interface's requirement for a CompareTo() method:

    int CompareTo(Person otherPerson)

    My problem was that I was given the wrong syntax for declaring the CompareTo() method. My syntax for calling the CompareTo() method was fine.

    Thanks again for your help!

    0 comments No comments