C#
  Home arrow C# arrow Building C# Comparable Objects: IComparabl...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Building C# Comparable Objects: IComparable versus IComparer
By: Ayad Boudiab
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-08-04

    Table of Contents:
  • Building C# Comparable Objects: IComparable versus IComparer
  • Sorting Objects
  • Quick Recap
  • Full Program

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building C# Comparable Objects: IComparable versus IComparer


    (Page 1 of 4 )

    Working with object-oriented languages (like C#) is all about manipulating objects. We create objects, change objects properties, save objects states, and sort objects. The part we are concentrating on in this article is sorting objects.

    Objects that are part of the language (such as strings) can be sorted without any additional work on our end. But when it comes to classes that we create (Book, Employee, Car…), we need to give hints to the runtime on how objects of these classes can be sorted (what does it mean that book1 comes before book2? Are they sorted by title? By ISBN?...). The hints that we give to the runtime are the interfaces. We implement interfaces such as IComparable and IComparer so the runtime can tell how to compare and sort objects.

    Interface definition: An interface is a named collection of abstract methods. Abstract means that the methods are not implemented. It is left up to the user of the interface to implement these methods the way he/she sees fit. When you create an interface you are creating a contract, which means that any code implementing the interface needs to implement all the methods in the interface (you simply cannot pick and choose which methods to implement).

    To create an interface you use the keyword interface followed by the name of the interface. Interfaces do not specify a base class, and all interface members are implicitly public. Here is an interface declaration:

    interface   IDraw

    {

    void Draw();

    }

    By convention, interface names are prefixed by the letter ‘I’. Based on this declaration, any class that needs to implement the interface IDraw must provide implementation for the method Draw(). Let’s create a class named Circle that implements the IDraw Interface:

    class   Circle : IDraw

    {

    public   override   void Draw()

    {

    Console .WriteLine( "Drawing a circle." );

    }

    }

    More C# Articles
    More By Ayad Boudiab


       · Welcome to my latest C# article. I am looking forward to your feedback.
     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT