Understanding Operator Overloading in C#
page 3 of 5
by Joydip Kanjilal
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 30735/ 51

Implementing Operator Overloading in C#

This section discusses how we can implement operator overloading in C#. Imagine you have a class called Fps that encapsulates some of the foot pound second operations, i.e., you can add two instances of type Fps, compare whether one is greater than the other, increment or decrement an instance, etc.

The following is the source code for the Fps class.

Listing 1

class Fps
{
  private int feet, inch;
 
  public Fps()
  {
  
  }
 
  public Fps(int f, int i)
  {
      feet = f;
      inch = i;
  }
//Some code
}

Suppose you want to add two Fps class instances and create a new instance out of it. Now, with Fps being a class, you cannot use this sort of operation on instances of this class unless you overload the + operator. The overloaded operator method in this case would be named operator+. This being a binary operator, you would be required to pass two parameters to this method to operate. The following code snippet illustrates how the binary operators +,-,> or < can be overloaded.

Listing 2

public static Fps operator + (Fps obj1, Fps obj2)
{
  Fps temp = new Fps();
  temp.feet = obj1.feet + obj2.feet;
  temp.inch = obj1.inch + obj2.inch;
 
  if (temp.inch > 12)
  {
    temp.feet++;
    temp.inch -= 12;
  }
 
  return temp;
}
 
public static Fps operator - (Fps obj1, Fps obj2)
{
  int value1 = obj1.feet * 12+obj1.inch;
  int value2 = obj2.feet * 12+obj2.inch;
  int value3 = value1 - value2;
  return new Fps(value3 / 12, value3 % 12);
  ;
}
 
public static bool operator > (Fps obj1, Fps obj2)
{
  int value1 = obj1.feet * 12+obj1.inch;
  int value2 = obj2.feet * 12+obj2.inch;
 
  if (value1 > value2)
    return true;
  return false;
}
 
public static bool operator < (Fps obj1, Fps obj2)
{
  int value1 = obj1.feet * 12+obj1.inch;
  int value2 = obj2.feet * 12+obj2.inch;
 
  if (value1 < value2)
    return true;
  return false;
}
            

Similarly, you could use the unary ++ or the -- operator to increment or decrement an instance of this class by one. These being unary operators, you require only one argument to the operator++ or the operator-- overloaded methods. The following code snippet shows how the unary operators ++ and -- can be overloaded.

Listing 3

public static Fps operator++(Fps obj)
{
  obj.inch++;
 
  if (obj.inch > 12)
  {
    obj.feet++;
    obj.inch -= 12;
  }
 
  return obj;
}
 
public static Fps operator--(Fps obj)
{
  obj.inch--;
 
  if (obj.inch < 0)
  {
    obj.feet--;
    obj.inch += 12;
  }
 
  return obj;
}

You can also use the implicit operator to convert an integer value to an Fps instance. You can know more on using the implicit operator in C# at the following link.

Understanding implicit operator overloading in C#

The following code snippet illustrates how the implicit operator can be used.

Listing 4

public static implicit operator Fps(int x)
{
  Fps f = new Fps();
  f.feet = x / 12;
  f.inch = x % 12;
  return f;
}

The following is the complete source code for the Fps class.

Listing 5

using System;
 
namespace OperatorOverloading
{
  class Fps
  {
    private int feet, inch;
 
    public Fps(){
 
    }
 
    public Fps(int f, int i)
    {
      feet = f;
      inch = i;
    }
 
    //Unary Operators
    public static Fps operator++(Fps obj)
    {
      obj.inch++;
 
      if (obj.inch > 12)
      {
        obj.feet++;
        obj.inch -= 12;
      }
 
      return obj;
    }
 
    public static Fps operator--(Fps obj)
    {
      obj.inch--;
 
      if (obj.inch < 0)
      {
        obj.feet--;
        obj.inch += 12;
      }
 
      return obj;
    }
 
    //Binary Operators
    public static Fps operator + (Fps obj1, Fps obj2)
    {
      Fps temp = new Fps();
      temp.feet = obj1.feet + obj2.feet;
      temp.inch = obj1.inch + obj2.inch;
 
      if (temp.inch > 12)
      {
        temp.feet++;
        temp.inch -= 12;
      }
 
      return temp;
    }
 
    public static Fps operator - (Fps obj1, Fps obj2)
    {
      int value1 = obj1.feet * 12+obj1.inch;
      int value2 = obj2.feet * 12+obj2.inch;
      int value3 = value1 - value2;
      return new Fps(value3 / 12, value3 % 12);
      ;
    }
 
    public static bool operator > (Fps obj1, Fps obj2)
    {
      int value1 = obj1.feet * 12+obj1.inch;
      int value2 = obj2.feet * 12+obj2.inch;
 
      if (value1 > value2)
        return true;
      return false;
    }
 
    public static bool operator < (Fps obj1, Fps obj2)
    {
      int value1 = obj1.feet * 12+obj1.inch;
      int value2 = obj2.feet * 12+obj2.inch;
 
      if (value1 < value2)
        return true;
      return false;
    }
 
    //Implicit conversion operator
    public static implicit operator Fps(int x)
    {
      Fps f = new Fps();
      f.feet = x / 12;
      f.inch = x % 12;
      return f;
    }
 
    public override string ToString()
    {
      return ("Feet = " + this.feet + "  Inch = " + this.inch);
    }
  }
}

Let us now use this class to perform the various operations that we discussed about. Refer to the code snippet below:

Listing 6

Fps f1 = new Fps(5,2); //Create new Fps instance
Console.WriteLine(f1.ToString());
Fps f2 = new Fps(2,11); //Create another Fps instance
Console.WriteLine(f2.ToString());
Fps f3 = f1 + f2; //Binary operation
Console.WriteLine(f3.ToString());
f3++; //Unary operation
Console.WriteLine(f3.ToString());
Fps f4 = 100; //Perform an explicit cast from integer to an Fps instance
Console.WriteLine(f4.ToString());

View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 12:53:11 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search