Use of Operator Overloading in C – Sharp

The following code snippet shows the use of operator overloading:
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication13
{
    class distance
    {
        int dist;
        public distance(int d )
        {
            this.dist=d;
        }
        public static distance operator +(distance d1, distance d2)
        {
            return new distance (d1.dist +d2.dist);
        }
    }
    class program
    {
        static void Main(string[] args)
        {
            distance d1 = new distance(3);
            distance d2 = new distance(10);
            distance d3 = new distance(0);
            d3 = d1 + d2;
            int a =(int)d3;
            Console.WriteLine(a);
            Console.ReadLine();
        }
    }
}

Add Comment