Comparision Operator Overloading C# with Example



Comparision Operator Overloading C# with Example

 	using System;

	namespace CSharpClass
	{
		class Time
		{
			byte hour {get; set;}
			byte minute {get; set;}
			
			public Time(){}
			public Time(byte h, byte m)
			{
				this.hour = h;
				this.minute = m;
			}
			
			public static bool operator <(Time operand1, Time operand2)
			{
				int totalMinutes1 = operand1.hour * 60 + operand1.minute;
				int totalMinutes2 = operand2.hour * 60 + operand2.minute;
				
				if(totalMinutes1 < totalMinutes2)
					return true;
				else
					return false;
			}
			
			public static bool operator >(Time operand1, Time operand2)
			{
				int totalMinutes1 = operand1.hour * 60 + operand1.minute;
				int totalMinutes2 = operand2.hour * 60 + operand2.minute;
				
				if(totalMinutes1 > totalMinutes2)
					return true;
				else
					return false;
			}
		}

		class OperatorOverloading
		{
			static void Main()
			{
				Time hmt = new Time(12, 10);
				Time sonata = new Time(23, 30);
				
				if(hmt < sonata)
					Console.WriteLine("Time of SONATA is Greater then HMT");
				else
					Console.WriteLine("Time of HMT is Greater then SONATA");
			}
		}
	} 

0 Comment's

Comment Form