Blog

Filter posts by Category Or Tag of the Blog section!

Working with operators in C#

Tuesday, 31 December 2013

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 it, 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:

 

    public class Calculator
    {
        public int Value { get; set; }
    }

 

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

 

        public static Calculator Plus()
        {
            var calculator1 = new Calculator {Value = 4};
            var calculator2 = new Calculator {Value = 1};
            var sum = calculator1 + calculator2;
            return sum;
        }

 

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 of different kinds of an operator:

 

    public class Calculator
    {
        public Calculator Value;

        public static Calculator operator +(Calculator a, Calculator b)
        {
            var calculator = new Calculator { Value = a.Value + b.Value };
            return calculator;
        }

        public static Calculator operator -(Calculator a, Calculator b)
        {
            var calculator = new Calculator { Value = a.Value - b.Value };
            return calculator;
        }

        public static Calculator operator *(Calculator a, Calculator b)
        {
            var calculator = new Calculator { Value = a.Value * b.Value };
            return calculator;
        }

        public static Calculator operator /(Calculator a, Calculator b)
        {
            var calculator = new Calculator { Value = a.Value / b.Value };
            return calculator;
        }

        public static bool operator >(Calculator a, Calculator b)
        {
            return a.Value > b.Value;
        }

        public static bool operator <(Calculator a, Calculator b)
        {
            return a.Value < b.Value;
        }
    }

 

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

 

        public static bool operator ==(Calculator a, Calculator b)
        {
            return b != null && (a != null && a.Value == b.Value);
        }

        public static bool operator !=(Calculator a, Calculator b)
        {
            return b != null && (a != null && a.Value != b.Value);
        }

 

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

 

        protected bool Equals(Calculator other)
        {
            return Value == other.Value && Equals(calculator, other.calculator);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((Calculator)obj);
        }


        public override int GetHashCode()
        {
            unchecked
            {
                return (Value * 397) ^ (calculator != null ? calculator.GetHashCode() : 0);
            }
        }

 

Have a good time, and happy 2014!

Category: Software

Tags: C#

comments powered by Disqus