Parameterized Constructor C# with Example



Parameterized Constructor 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);
			}
		}
			
			class UsingParameterizedConstructor{
				static void Main(String[] arg){
					Watch hmt = new Watch();
					Console.Write("Time of the HMT Watch is: ");
					hmt.DisplayTime();
					
					Watch sonata = new Watch(10,12);
					Console.Write("Time of the Sonata Watch is: ");
					sonata.DisplayTime();
				}
			}
		}
 

0 Comment's

Comment Form

Submit Comment