How and when to Use Interfaces like IComparable

This article is targeted for the people who don't know how to use Interfaces .

In layman sense an interface is a class containing methods without body and without body methods that we can implement in our class.

if methods having no body then what is use of it? we can also directly write method in our class without implementing interface.

Above question will always exist for the user who doesn't know when and how to use an interface. Lets take one example to explain this to you. Array class containing method that that sorts Arrays that are of Primitive type int , float etc. but if you try to sort Array of Custom Type you have developed with Array.Sort() is it going to work ? Well it will not going to work for it. So how to tell Sort() method How to Compare the Data Inside your custom type ? and Sort According to your custom conditions? Here comes the interface in picture. Array.Sort() method uses CompareTo() method to Compare Two objects for sorting purpose so if we have interface that contain CompareTo() method than we can override that method to provide our own logic for comparison of object and it will be taken in use by Array.Sort() method. CompareTo() method returns int according to comparison logic inside.

So now you are familiar with how and when the interfaces are used :)

Lets use the Interface Practically. I will show you how to Sort Student DataType Array with IComparable.

If we implement IComparable interface that means that our Type is Comparable with operator like > < >= etc

I have Created a Class Student like below that Implements Interface IComparable

class Student :IComparable
        private string _Name;
        private int _Age;
        private string _Course;

        public Student(string Name,int Age,string Course)
            _Name = Name;
            _Age = Age;
            _Course = Course;
        public String Name
            get{return _Name;}
            set{_Name = value;}

        public int Age
            get{return _Age;}
            set { _Age = value; }


        public string Course
            get { return _Course; }
            set { _Course = value; }

        public void PrintStudentDetail()
            Console.WriteLine("Name :" + _Name);
            Console.WriteLine("Age :" + _Age);
            Console.WriteLine("Course:" + _Course);

        /* Method to Override in IComparable Interface */
        public int CompareTo(Object obj)
            Student s = (Student)obj;
            return s.Name.CompareTo(_Name);

Here I created a Class Named Student that contain its Basic properties for getting and setting its instance variables

I have Implemented IComparable Interface by :IComparable after class name . now to make Capable our Student Type comparable we have overrided

CompareTo()  method in our class and Provided custom logic like we are sorting Our Object From Student Names .Now when we call Method Array.Sort() on our

Custom DataType Array it will sort out DataType according to Names .

lets see its Demonstration by using out created class

  static void Main(string[] args)
            Student[] SArray = new Student[5];
            SArray[0] = new Student("kirtan", 22, "MCA");
            SArray[1] = new Student("ghanashyam", 21, "MCA");
            SArray[2] = new Student("sanjay", 21, "MCA");
            SArray[3] = new Student("jay", 22, "BCA");
            SArray[4] = new Student("mahesh", 25, "MTech");


            Console.WriteLine(":: Sorted by Implementing ICompatable Interface ::");
            foreach (Student s in SArray)


and Here is out put in which we have sorted our custom datatype using Sort() method .

9-9-2010 11-17-14 PM.gif