Ehsan Ghanbari

Experience, DotNet, Solutions

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 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!

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
Peridic pattern
How to use PagedList In asp.net MVC
Redis as a cache server
Domain driven design VS model driven architecture
How to query over Icollection<> of a type with linq
What's the DDD-lite?
comments powered by Disqus