Overloading the Relational and Logical Operators - CSharp Custom Type

CSharp examples for Custom Type:Operator Overloading

Introduction

The relational operators can also be overloaded. This includes the following operators:

  • <
  • <=
  • >
  • >=

This also includes the logical operators:

  • ==
  • !=

Demo Code

using System;// w ww  . j a  v  a2 s  .c  om
using System.Text;
public class Salary
{
   private int AMT;
   public Salary() { this.amount = 0; }
   public Salary(int val) { this.amount = val; }
   public int amount
   {
      get{ return this.AMT; }
      set{ this.AMT = value; }
   }
   static public bool operator < ( Salary first, Salary second )
   {
      bool retval;
      if ( first.amount < second.amount )
         retval = true;
      else
         retval = false;
         return retval;
      }
      static public bool operator <= ( Salary first, Salary second )
      {
         bool retval;
         if ( first.amount <= second.amount )
            retval = true;
         else
            retval = false;
            return retval;
         }
         static public bool operator > ( Salary first, Salary second )
         {
            bool retval;
            if ( first.amount > second.amount )
               retval = true;
            else
               retval = false;
               return retval;
            }
            static public bool operator >= ( Salary first, Salary second )
            {
               bool retval;
               if ( first.amount >= second.amount )
                  retval = true;
               else
                  retval = false;
                  return retval;
               }
               public override string ToString()
               {
                  return( this.amount.ToString() );
               }
            }
            public class myAppClass
            {
               public static void Main(String[] args)
               {
                  Salary mySalary   = new Salary(24000);
                  Salary yourSalary = new Salary(24000);
                  Salary PresSalary = new Salary(200000);
                  Console.WriteLine("Original values: ");
                  Console.WriteLine("      my salary: {0}", mySalary);
                  Console.WriteLine("    your salary: {0}", yourSalary);
                  Console.WriteLine(" a Pres' salary: {0}", PresSalary);
                  if ( mySalary < yourSalary )
                     Console.WriteLine("My salary less than your salary");
                  else if ( mySalary > yourSalary )
                  Console.WriteLine("My salary is greater than your salary");
                  else
                     Console.WriteLine("Our Salaries are the same");
                     if ( mySalary >= PresSalary )
                        Console.WriteLine("\nI make as much or more than a president.");
                     else
                        Console.WriteLine("\nI don't make as much as a president.");
                     }
}

Result


Related Tutorials