Sunday, April 19, 2015

Structure and Operator overloading in C#

Recently I have been playing with structure and It’s fun. In this blog post we are going to learn how we can use operator overloading with structure in C#.  You can find my blog post that I have already written about structure  at following location.

Structure in C#
Structure with constructor in C#
Structure with Interface in C#

So as usual I've created a console application to demonstrate operator overloading in C#. Here I have created a structure with equals operator overloading and following is a code for that.

namespace StrcutureAndOperatorOverloading
{
    public struct Employee
    {
        public int EmployeeId { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = 17;
                result = result * 23 + this.EmployeeId.GetHashCode();
                result = result * 23 + ((FirstName != null) ? this.FirstName.GetHashCode() : 0);
                result = result * 23 + ((LastName != null) ? this.LastName.GetHashCode() : 0);
                return result;
            }
        }

        public override bool Equals(object obj)
        {
            if (obj is Employee)
            {
                return this.Equal((Employee)obj);
            }
            return false;
        }

        private bool Equal(Employee empoyee)
        {
            return (this.EmployeeId == empoyee.EmployeeId) &&
                (this.FirstName == empoyee.FirstName) &&
                (this.LastName == empoyee.LastName);
        }
    }
}
If you see this I have created a structure Employee where I have created  few properties to represent employee properties and then I have written a private equal method to check whether current employee and another employee are equal or not?. If it is equal then I have written true.

Then I have written operator overloading method Equals which takes object as argument. First it will check whether object passed is structure or not otherwise it will return false. If object is Employee structure then it will call equal private method.

I have also override GetHashCode method as this is a requirement of Equals operator overloading. That’s it. We are done with operator overloading and now let’s check whether this code works fine or not with our console application.
using System;

namespace StrcutureAndOperatorOverloading
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee employee = new Employee
            {
                EmployeeId=1,
                FirstName="Jalpesh",
                LastName="Vadgama"
            };

            Employee anotherEmployee = new Employee
            {
                EmployeeId=2,
                FirstName="Vishal",
                LastName="Vadgama"
            };

            bool isEqual = employee.Equals(anotherEmployee);
            Console.WriteLine("Checking equal operator with Structure");
            Console.WriteLine(isEqual);
        }
    }
}
In Main method of console application, I have created two employee object and then checked with equals operator whether both are same or not? And then I printed the result with Console.Writeline method. Now let’s run this program to check whether it’s working fine or not. Following is a output as expected.

structure-with-operator-overloading

That’s it. Hope you like it. Stay tuned for more!.

You can find this blog post and structure series blog post example on Github at - https://github.com/dotnetjalps/StructureCSharp
Share:

2 comments:

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews