Tuesday, November 30, 2010

A Program that illustrates basics of Operator overloading in C#

I developed this class in C# to illustrate the basics of Operator overloading and Typecasting This sample also contains a use of the ToString method. This example is divided into three parts:-

 

Program.cs
//************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Operator_Overloading
{
    class Program
    {
        static void Main(string[] args)
        {
            Currency c1 = new Currency(-12, 234);
            Currency c2 = new Currency(12, 234);
            Currency sum = c1 + c2;
            Console.WriteLine(sum );
            if (c1 < c2)
                Console.WriteLine(c1);
            else
                Console.WriteLine(c2);
            double d = c1;
            Console.WriteLine(d);
            int n =(int) c1;
            Console.WriteLine(n);
            Console.WriteLine(c1["rupees"]);
            Console.ReadKey();
        }
    }
}

//************************************************************************************
Currency,cs
//**********************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Operator_Overloading
{
  public partial   class Currency
    {
      public int this[string n]
      {
          get
          {
              n = n.Trim().ToLower();
              int p = 2;
              if (n.Equals("rs") || n.Equals("rupees"))
                  p = 0;
              if (n.Equals("paise") || n.Equals("p"))
                  p = 1;
              return this[p];
          }
      }
      public int this[int n]
      {
          get
          {
              if (n == 0)
                  return paise / 100;
              if (n == 1)
                  return paise % 100;
              throw new Exception("Index out of range");
          }
      }

      public static Currency operator +(Currency c1, Currency c2)
      {
          return new Currency(c1.paise + c2.paise);
      }


      private int paise;
      public Currency()
      {
          this.paise = 0;
      }
      private Currency(int paise)
      {
          this.paise = paise;
      }
      public Currency(int rupees, int paise)
      {
          this.paise = 100 * rupees + paise;
      }


      public override string ToString()
      {
          string s;
          if (paise < 0)
          {
              s = "-";
              paise = -paise;
          }
          else
              s = "";
          int r = paise / 100;
          int p = paise % 100;
          string ps;
          if(p<=9)
          ps="0" + p;
          else
              ps="" + p;
          return s + "Rs "   + r + "." + ps;
      }
    }
}

//**********************************************************************************
OperatorsOfCurrency.cs
//**********************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Operator_Overloading
{
  public partial   class Currency
    {
      public static explicit  operator int(Currency c)
      {
          return Convert.ToInt32 ( Math .Round(c.paise/100.0,0));
      }
      public static  implicit operator   double(Currency c)
      {
          return c.paise /100.0;
      }
      public static bool operator <(Currency c1, Currency c2)
      {
          if (c1.paise < c2.paise)
              return true;
          else
              return false;
      }
public static bool operator >(Currency c1, Currency c2)
      {
          if (c1.paise > c2.paise)
              return true;
          else
              return false;
      }
    }
}

//**********************************************************************************

1 comment: