C# Program to Implement IComparable Interface
Last Updated :
18 Feb, 2022
C# provides an IComparable interface. This interface provides different types of type-specific comparison methods which means a value type or a class can implement this interface to sort its instances because we cannot sort the instances of a class directly because the compiler does not know on which basis to sort. Similarly comparing directly two instances will throw compiler error as the compiler gets confused about what to compare.
Syntax:
public interface IComparable
Implementing IComparable Interface requires:
- Adding a method CompareTo() which receives an object and returns an integer.
- The incoming object is first type-casted as the class type and stored in a temporary variable. It is then compared with a property of the current method
- The CompareTo() method depending on the comparison:
- returns 0, if the current instance's property is equal to the temporary variable's property
- returns 1, if the current instance's property is greater than the temporary variable's property
- returns -1, if the current instance's property is less than the temporary variable's property
Now let us discuss how to implement the IComparable interface with the help of the below example.
Example: Below is the implementation of the IComparable interface. in this example we sort the employees according to their employee ID.
C#
// C# program to demonstrate how to
// implement IComparable interface
using System;
using System.Collections.Generic;
class GFG{
// Driver code
static public void Main()
{
// Create an array of employees
// using collection initializer
Employee[] employees = new Employee[]
{
new Employee(1, "Susmita"),
new Employee(5, "Soniya"),
new Employee(3, "Rohit"),
new Employee(2, "Mohit"),
new Employee(4, "Pihu")
};
// Displaying the employee's array before sorting
Console.WriteLine("Before sorting employees array");
foreach(var emp in employees)
{
Console.WriteLine("ID - {0}, Employee Name - {1}",
emp.ID, emp.EmployeeName);
}
// Sorts the employees array in ascending
// order on basis of id of the employee
Array.Sort(employees);
Console.WriteLine();
// Printing the employee's array after sorting
Console.WriteLine("After sorting employees array");
foreach(var emp in employees)
{
Console.WriteLine("ID - {0}, Employee Name - {1}",
emp.ID, emp.EmployeeName);
}
}
}
// Implementing IComparable interface
public class Employee : IComparable{
public int ID;
public string EmployeeName;
// Employee constructor
public Employee(int id, string employeename)
{
this.ID = id;
this.EmployeeName = employeename;
}
// Implementation of the CompareTo() method which takes
// an object as an input and return integer value depending on
// the comparison between current object and incoming object,
public int CompareTo(object incomingobject)
{
// Storing incoming object in temp variable of
// current class type
Employee incomingemployee = incomingobject as Employee;
return this.ID.CompareTo(incomingemployee.ID);
}
}
Output:
Before sorting employees array
ID - 1, Employee Name - Susmita
ID - 5, Employee Name - Soniya
ID - 3, Employee Name - Rohit
ID - 2, Employee Name - Mohit
ID - 4, Employee Name - Pihu
After sorting employees array
ID - 1, Employee Name - Susmita
ID - 2, Employee Name - Mohit
ID - 3, Employee Name - Rohit
ID - 4, Employee Name - Pihu
ID - 5, Employee Name - Soniya
Similar Reads
C# Program to Demonstrate the IList Interface In C# IList interface is an interface that belongs to the collection module where we can access each element by index. Or we can say that it is a collection of objects that are used to access each element individually with the help of an index. It is of both generic and non-generic types. Its implem
3 min read
C# Program to Demonstrate the IDictionary Interface IDictionary Interface is an interface that belongs to the collection module where we can access the elements by keys. Or we can say that the IDictionary interface is a collection of key/value pairs. It is available for both generic and non-generic types collection. Here each pair must contain a uniq
3 min read
C# Program to Implement Multiple-Inheritance using Abstract Class and Interface Given multiple inheritance, now our task is to implement multiple inheritance with the help of abstract class and interface. So, before implementation first of all we learn the basic definition of multiple-inheritance, abstract class, and interface. Multiple inheritance: Multiple inheritance is a ty
4 min read
C# Program to Demonstrate the Inheritance of Abstract Classes Abstraction is the process to hide the internal details and show only the functionality. The abstract keyword is used before the class or method to declare the class or method as abstract. And inheritance is the object-oriented programming methodology by which one class is allowed to inherit the fea
2 min read
C# Program to Check a Specified Type is an Interface or not The interface is just like a class, it can also have methods, properties, events, etc. as its members, but it only contains the declaration of the members and the implementation of these members will be given by the class that implements the interface implicitly or explicitly. We can check the speci
2 min read
C# Program to Check a Specified Class is an Abstract Class or Not Abstraction is the process to hide the internal details and showing only the functionality. The abstract keyword is used before the class or method to declare the class or method as abstract. In this article, we will learn how to check a specified class is an abstract class or not. To do this task w
2 min read