C# Program to Implement IComparable Interface

This is a C# Program to implement icomparable interface.

Problem Description

This C# Program Implements IComparable Interface.

Problem Solution

IComparable allows custom sorting of objects when implemented. When a class implements this interface, we must add the public method CompareTo(T).

Program/Source Code

Here is source code of the C# Program to Implement IComparable Interface. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Implement IComparable Interface
 */
using System;
class Fraction : IComparable
{
    int z, n;
 
    public Fraction(int z, int n)
    {
        this.z = z; this.n = n;
    }
 
    public static Fraction operator +(Fraction a, Fraction b)
    {
        return new Fraction(a.z * b.n + a.n * b.z, a.n * b.n);
    }
 
    public static Fraction operator *(Fraction a, Fraction b)
    {
        return new Fraction(a.z * b.z, a.n * b.n);
    }
 
    public int CompareTo(object obj)
    {
        Fraction f = (Fraction)obj;
        if ((float)z / n < (float)f.z / f.n) 
          return -1;
        else if ((float)z / n > (float)f.z / f.n) 
           return 1;
        else return 0;
    }
 
    public override string ToString()
    {
        return z + "/" + n;
    }
}
 
class Test
{
 
   static void Main(string[] arg)
   {
      Fraction[] a = 
      {
      new Fraction(5,2),
      new Fraction(29,6),
      new Fraction(4,5),
      new Fraction(10,8),
      new Fraction(34,7)
      };
        Array.Sort(a);
        Console.WriteLine("Implementing the IComparable Interface in " + 
                          "Displaying Fractions : ");
        foreach (Fraction f in a) Console.WriteLine(f + " ");
        Console.WriteLine();
        Console.ReadLine();
   }
 
}
Program Explanation

This C# program is used to implement IComparable interface. Implement the IComparable interface for printing fractions. IComparable allows custom sorting of objects when implemented.

advertisement
advertisement

When a class implements this interface, add the public method CompareTo(T). CompareTo(T others)”. The generic version of this interface provides a more type-safe manner to handle the comparison between objects. Where “T” is the type of object and “other” is an object to compare with the current object.

The role of IComparable is to provide a method to compare two objects of a particular type. This is necessary if we want to provide any ordering capability for the object.

Runtime Test Cases
 
Implementing the IComparable Interface in Displaying Fractions : 
4/5
10/8
5/2
29/6
34/7

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.