Home / Articles / Programming Languages / C# / C# Collection Classes III

“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.

C# Programming Series

C# Collection Classes III

Yashavant Kanetkar
Yashavant Kanetkar

Index

  1. The IComparer Interface
  2. Other Collection Classes
The use of appropriate data structures in the right places goes a long way toward building efficient programs. If you use an array in place of a linked list or vice-versa, a program is likely to misbehave in some situations. However, when writing a C# program, the last thing on one's mind is how to build a linked list. There are more important things to attend to. .NET Collection Classes can be used to manage data structures. .NET provides the System.Collections namespace that contains various interfaces and classes representing collections of objects. For example, lists, queues, arrays, hashtables and dictionaries. Let us discuss these classes and interfaces one by one.

The IComparer Interface

The IComparer interface provides methods to compare two objects. Most of the classes implement this interface so that the user can easily compare two objects of the class.

Let us create a program that shows how to implement the IComparer interface. In this program we would write a class emp maintaining the information of employees. Another class mysort would implement the IComparer interface. Here is the program…

using System ;
using System.Collections ; 

namespace icomb
{
    class emp 
    {
        public string name ;
        public int id ;
        public float balance ;

        public emp ( int i, string n, float b ) 
        {
            id = i ;
            name = n ;
            balance = b ;
        }
        
        public new string ToString( )
        {
            return id + " " + name + " " + balance ;
        }
    }

    public class mysort : IComparer
    {
        public int Compare ( object a, object b )
        {
            int i1 = ( ( emp ) a ).id ;
            int i2 = ( ( emp ) b ).id ;
            string n1 = ( ( emp ) a ).name ;
            string n2 = ( ( emp ) b ).name ;

            if ( i1 == i2 )
            {
                return n1.CompareTo ( n2 ) ;
            }

            if ( i1 < i2 )
                return -1 ;
        
            return 1 ;
        }
    }

    public class Class1
    {
        public static int Main ( string[ ] args )
        {
            emp[ ] e = 
            {
                new emp ( 2, "Sanjay", 3450 ),
                new emp ( 1, "Rahul", 2500 ) ,
                new emp ( 10, "Kavita", 10000 ),
                new emp ( 9, "Mohit", 25000),
                new emp ( 6, "Sapna", 2500),
            } ;

            mysort s = new mysort( ) ;
            Array.Sort ( e, s ) ;
            foreach ( emp s1 in e )
            Console.WriteLine ( s1.ToString( ) ) ;
            
            return 0 ;
        }
    }
}

Here, we have firstly instantiated an array e of type emp and then an object of type mysort. In the mysort class we have defined the Compare( ) method of the IComparer interface and written our own sorting logic in it. Next, to sort the array e we have called the Sort( ) method of the Array class. To the Sort( ) method we have to pass the array to be sorted and reference to the IComparer interface. The Sort( ) method calls the Compare( ) method. The Compare( ) method returns 0, 1 or -1 indicating whether the objects being compared are equal or one is smaller than the other. In our case, since the mysort class is derived from IComparer we have passed reference to the mysort's object. Hence, the Compare( ) method of mysort class gets called.

Once the array is sorted we have displayed the array elements. We have overridden the ToString( ) method in the emp class. This is because we wanted to display the sorted elements using the statement s1.ToString( ). Had we not overridden this method in emp class, ToString( ) of the base class would have got called, which we do not want. In the ToString( ) of emp class we have concatenated the values of the data members in a string and returned it.

Comments

Log in or create a user account to comment.

On Sale From April 2008

Let Us C
8th Ed.
C programming classic & best seller. 1 million+ copies sold!

Y. Kanetkar

On Sale From April 2008

Introduction to Object Oriented Programming & C++

Y. Kanetkar

On Sale From Fall 2008

Microsoft .NET Framework: Web Application Security

Vijay Mukhi

On Sale From Nolvember 2008

Quest C++ Courseware
12+ hours of instructional audio and animated slides.

Y. Kanetkar Asang Dani

On Sale From November 2008

A Programmer's Guide to Web Application Security

Vijay Mukhi

Latest Forum Posts