Friday 6 April 2012

EXAMPLE OF ICOMPARABLE INTERFACE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    public class Employee : IComparable
    {
        private int d, c;
        public Employee(int e, int f)
        {
            this.d = e;
            this.c = f;
        }
        public int CompareTo(Employee other)
        {
            int thisvalue = (this.d * 100) + this.c;
            int othervalue = (other.d * 100) + other.c;
            return thisvalue.CompareTo(othervalue);
        }
        public override string ToString()
        {
            // String representation.
            return this.d.ToString() + "," + this.c;
        }
        static void Main()
        {
            Employee[] sal ={new Employee(100,0),
                          new Employee(57,23),
                          new Employee(250,97)};
            Array.Sort(sal);
            // Uses IComparable.CompareTo()
            // Uses Employee.ToString
            foreach (var element in sal)
            {
                Console.WriteLine(element);
            }
        }
    }
}

COMPARE TO METHOD

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    class Program
    {
        int a, b;
        public int method()
        {
            a = 3; b = 3;
            return b.CompareTo(a);
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            int c = p.method();
            Console.Write(c);

        }
    }
}


No comments:

Post a Comment