Behind the Scenes Look at C#: Operators, continued - Operator overloading, revisited
(Page 5 of 5 )
In our example it makes sense to use the addition operator + with two instances of the Number class, so we need to overload the + operator to do that.
using System;
namespace Operators
{
class Class1
{
static void Main(string[] args)
{
Number i = new Number();
i.aNumber = 90;
i.aDecimal = .5m;
Number o = new Number();
o.aNumber = 10;
o.aDecimal = .3m;
Number SumNumber = i + o;
Console.WriteLine("The value of i + o = {0}", SumNumber.aNumber);
Console.ReadLine();
}
}
public class Number
{
public int aNumber;
public decimal aDecimal;
public static Number operator+(Number num, Number num2)
{
Number temp = new Number();
temp.aNumber = num.aNumber + num2.aNumber;
return temp;
}
public override string ToString()
{
return Convert.ToString(this.aNumber + this.aDecimal);
}
}
}
The result will be:

The ability to express basic operations on user-defined types is very unlimited and complex. Operator overloading is one feature that C# has over VB.NET. Usually when you overload an operator you will overload its family. In other words, when you overload the addition operator + you will need to overload all of the rest of the arithmetic operators (*, /, - and %). The compiler will not issue a warning if you overload only one of these operators, but it does make sense to overload all of them.
Why can you add a Number instance to another one, but you can't subtract them? If you have overloaded the > operator, it makes sense to overload the < operator, and it will be perfect if you overload the >= and the <= operators. The C# compiler will not let you overload only one operator without overloading the other one (this is different from the arithmetic operators). Let's complete our example by overloading the whole arithmetic operators family.
using System;
namespace Operators
{
class Class1
{
static void Main(string[] args)
{
Number i = new Number();
i.aNumber = 90;
i.aDecimal = .5m;
Number o = new Number();
o.aNumber = 10;
o.aDecimal = .3m;
Number SumNumber = i + o;
Console.WriteLine("The value of i + o = {0}", SumNumber.aNumber);
SumNumber = i - o;
Console.WriteLine("The value of i - o = {0}", SumNumber.aNumber);
SumNumber = i * o;
Console.WriteLine("The value of i * o = {0}", SumNumber.aNumber);
SumNumber = i / o;
Console.WriteLine("The value of i / o = {0}", SumNumber.aNumber);
SumNumber = i % o;
Console.WriteLine("The value of i % o = {0}", SumNumber.aNumber);
Console.ReadLine();
}
}
public class Number
{
public int aNumber;
public decimal aDecimal;
public static Number operator+(Number num, Number num2)
{
Number temp = new Number();
temp.aNumber = num.aNumber + num2.aNumber;
return temp;
}
public static Number operator-(Number num, Number num2)
{
Number temp = new Number();
temp.aNumber = num.aNumber - num2.aNumber;
return temp;
}
public static Number operator*(Number num, Number num2)
{
Number temp = new Number();
temp.aNumber = num.aNumber * num2.aNumber;
return temp;
}
public static Number operator/(Number num, Number num2)
{
Number temp = new Number();
temp.aNumber = num.aNumber / num2.aNumber;
return temp;
}
public static Number operator%(Number num, Number num2)
{
Number temp = new Number();
temp.aNumber = num.aNumber % num2.aNumber;
return temp;
}
public override string ToString()
{
return Convert.ToString(this.aNumber + this.aDecimal);
}
}
}
Compile and run the code to get the following result to the console window:

| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |