Automatic Operator Overloading C# with Example



Automatic Operator Overloading 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 static Coordinate operator +(Coordinate operand1, Coordinate operand2)
			{
				Coordinate temp = new Coordinate();
				temp.x = operand1.x + operand2.x;
				temp.y = operand1.y + operand2.y;
				return temp;
			}
		}

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

0 Comment's

Comment Form

Submit Comment