Operator Overloading Basics C# with Example



Operator Overloading Basics C# with Example

 using System;

namespace CSharpClass
{
    class Coordinate
    {
		int x {get; set;}
		int y {get; set;}
		
		public Coordinate(){}
		public Coordinate(int x, int y)
		{
			this.x = x;
			this.y = y;
		}
		
		public void Position(string type)
		{
			Console.WriteLine("{0} Position: {1},{2}", type, x, y);
		}
		
		public Coordinate Plus(Coordinate operand)
		{
			Coordinate temp = new Coordinate();
			temp.x = this.x + operand.x;
			temp.y = this.y + operand.y;
			return temp;
		}
    }

    class OperatorOverloadingBasic
    {
        static void Main()
        {
			Coordinate pixel1 = new Coordinate(12,21);
			Coordinate pixel2 = new Coordinate(12,21);
			
			Coordinate calculated;
			calculated = pixel1.Plus(pixel2);
			calculated.Position("Actual Pixel");
        }
    }
} 

0 Comment's

Comment Form