Equals Overridden With Watch Class C# with Example



Equals Overridden With Watch Class C# with Example

 		using System;

		namespace CSharpClass{
		class Watch
		{
			public byte hour{set; get;}	
			public byte minute{set; get;}

			public Watch()
			{
				hour = 12;
				minute = 0;
			}
			
			public Watch(byte h, byte m)
			{
				hour = h;
				minute = m;
			}
			
			public void DisplayTime()
			{
				Console.WriteLine(hour + ":" + minute);
			}
			
			public override string ToString()
			{
				string time;
				time = string.Format("{0}:{1}", hour, minute);
				return time;
			}
			
/*			public override bool Equals(object x)
			{
				if(x is Watch && x != null)
				{
					Watch temp = (Watch)x;
					if(this.hour == temp.hour && this.minute == temp.minute)
						return true;
					else
						return false;
				}
				return false;
			}
*/
			public override int GetHashCode()
			{
				return hour.GetHashCode();
			}

			public override bool Equals(object x)
			{
				return x.ToString() == this.ToString();
			}
		}
			
			class UsingWatchConstructor{
				static void Main(String[] arg){
					Watch hmt = new Watch(10,12);
					Watch sonata = new Watch(10,12);

					Console.Write("Both Watch have Same Time: {0}\n", hmt.Equals(sonata));
				}
			}
		}
 

0 Comment's

Comment Form

Submit Comment