true-false- Operator Overloading C# with Example



true-false- 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 bool operator true(Coordinate operand)
			{
				if(operand.x != 0 && operand.y != 0)
					return true;
				else
					return false;
			}
			
			public static bool operator false(Coordinate operand)
			{
				if(operand.x == 0 && operand.y == 0)
					return true;
				else
					return false;
			}
		}

		class OperatorOverloadingBasic
		{
			static void Main()
			{
				Coordinate pixel1 = new Coordinate(12,21);
				Coordinate pixel2= new Coordinate(0,0);
				
				if(pixel1)
					Console.WriteLine("Coordinate is not 0,0 of pixel1");
				else
					Console.WriteLine("Coordinate is 0,0 of pixel1");
					
				if(pixel2)
					Console.WriteLine("Coordinate is not 0,0 of pixel2");
				else
					Console.WriteLine("Coordinate is 0,0 of pixel2");
			}
		}
	} 

0 Comment's

Comment Form

Submit Comment