Saturday, 18 February 2012

How to use IComparable Interface?

Yes we can compare custom objects(Employee,Car,Students)s also.before discuss How to compare 2 custom objects we will see from basic level of this type of comparisons.

Ex :   int  i = 100;
         int  j = 100;
            if (i = = j)
            {             
                Console.WriteLine("both are equal");
            }
           else
           {
                 Console. WriteLine ("both are not equal");
            }
ok well as strings also


Ex:       string str1 = "kasi";
            string str2 = "babu";
            if (str1 == str2)
            {
                Console.WriteLine("both are equal");
            }
            else
                Console.WriteLine("both are different strings");

            -----ok fine we can perform these comparsions using operators.but preferable way is using .net Pre-defined methods.
  Yes In dotnet we have 2 Methods 1.Equals(object obj)   and 2.CompareTo(object value).
methods
  but each one have their own purposes.
the above ex we can perform like below also and perfered way also.

1. Equals()

Equals method is defined in Object so every derived type of object implictly have this method.it can be int,float,string,double....ect
   class Object
  {
     public virtual bool Equals(object obj);
  }

Note: Every type in .net implictly derived from Object type(It can be Valuetype/ Referencetype)
Ex:
       int i=10;
       int j=10;

        if (i.Equals(j))
                Console.WriteLine("yes equal");

--------yes this also one  way of comparing 2 values.but Equal is return type is boolean which means returns true/ false  olny.

2. compareTo
yes we have one more method is called CompareTo() is used to compare 2 values(strings,integers,...ect).this method is defined in IComparable interface. we know whatever types implements this interface. they access this method implictly to compare 2 values.
like
           Int :IComparable
          string :IComparable
          float: IComparable
.................ect
Why this CompareTo method we have already have Equals() method know!
yes above stament question is right but Equals() ---mehtod does not return any value.
but comming to CompareTo method which will return 3 values as per comparing objects.

IF 2 objects are equal returns -------->0(zero)
Ex:
   int i=10,j=10;
   if(i.CompareTo(j)= =0)
    {
             console.WriteLine("both are equal");

    }
Now
Ex2:
   IF Comparing object( int i) has greater value then compared object(int j) then returns---1(one)

    int i=100,j=10;
   if(i.CompareTo(j)= =1)
    {
             console.WriteLine("comparing object(i) is greater then compared object(j)");
    }
Now

Ex3:
  IF comparing object(int i)has less value then compared object(int j) then returns----  -1(minus one)

       int i=10,j=100;
   if(i.CompareTo(j)= =-1)
    {
             console.WriteLine("compareing object(i) is less then compared object");
    }

       ok.fine.this the way of comparison is so good and prefered way.

No comments:

Post a Comment