Working with operators in C#

As you know or maybe you have used thousands of times, there are some built in operators in C# and .Net for example mathematical operators on int, long and … but if in some cases you should create your own operator for your defined classes. You can do this by using operator keyword.

Imagine that you have this Class with an int property named Value:

  1.     public class Calculator
  2.     {
  3.         public int Value { get; set; }
  4.     }

Now, If you want to create a method for plus two members, everything will be OK:

  1.         public static Calculator Plus()
  2.         {
  3.             var calculator1 = new Calculator {Value = 4};
  4.             var calculator2 = new Calculator {Value = 1};
  5.             var sum = calculator1 + calculator2;
  6.             return sum;
  7.         }

But if you change the type of Value to Calculator, you will get the compile error as there is no any built in plus operator for your class(Calculator)! In these rare cases you should define your own operator to support different kinds of operation on your class based on your needs, here I mention some example about different kinds of operator:

  1.     public class Calculator
  2.     {
  3.         public Calculator Value;
  4.  
  5.         public static Calculator operator +(Calculator a, Calculator b)
  6.         {
  7.             var calculator = new Calculator { Value = a.Value + b.Value };
  8.             return calculator;
  9.         }
  10.  
  11.         public static Calculator operator -(Calculator a, Calculator b)
  12.         {
  13.             var calculator = new Calculator { Value = a.Value - b.Value };
  14.             return calculator;
  15.         }
  16.  
  17.         public static Calculator operator *(Calculator a, Calculator b)
  18.         {
  19.             var calculator = new Calculator { Value = a.Value * b.Value };
  20.             return calculator;
  21.         }
  22.  
  23.         public static Calculator operator /(Calculator a, Calculator b)
  24.         {
  25.             var calculator = new Calculator { Value = a.Value / b.Value };
  26.             return calculator;
  27.         }
  28.  
  29.         public static bool operator >(Calculator a, Calculator b)
  30.         {
  31.             return a.Value > b.Value;
  32.         }
  33.  
  34.         public static bool operator <(Calculator a, Calculator b)
  35.         {
  36.             return a.Value < b.Value;
  37.         }
  38.     }

Now you can simply create method to do + , *, -  And / operators are supporting them for your class. You can also add equality and non-equality operators:

  1.         public static bool operator ==(Calculator a, Calculator b)
  2.         {
  3.             return b != null && (a != null && a.Value == b.Value);
  4.         }
  5.  
  6.         public static bool operator !=(Calculator a, Calculator b)
  7.         {
  8.             return b != null && (a != null && a.Value != b.Value);
  9.         }

 but keep in mind in that you should override the Equals(object obj) and  GetHashCode() to defining the == and != operators:

  1.         protected bool Equals(Calculator other)
  2.         {
  3.             return Value == other.Value && Equals(calculator, other.calculator);
  4.         }
  5.  
  6.         public override bool Equals(object obj)
  7.         {
  8.             if (ReferenceEquals(null, obj)) return false;
  9.             if (ReferenceEquals(this, obj)) return true;
  10.             if (obj.GetType() != this.GetType()) return false;
  11.             return Equals((Calculator)obj);
  12.         }
  13.  
  14.  
  15.         public override int GetHashCode()
  16.         {
  17.             unchecked
  18.             {
  19.                 return (Value * 397) ^ (calculator != null ? calculator.GetHashCode() : 0);
  20.             }
  21.         }

 

Have a good time, and happy 2014!


Tags: C#


comments powered by Disqus