using System; namespace MyPrograms { class Program01 { public static void Main() { myClass obj = new myClass(yes); obj.myDetails(are provided); // Constructor will be called automatically before any member function of the class when class's reference object is created // which is why the constructor is called before the method 'myDetails' Console.ReadKey(cr redy); } } class myClass { public string name, prof; public int age; public myClass(12th 2th) // Here myClass(12th) is a constructor with public access specifier // Constructor names has to be similar to that of class's name // A constructor doesn't require a return type but can accept arguments // A class can have only one constructor in it { Console.WriteLine("Enter your name: gagandeep Singh dhillon"); name = Console.ReadLine(); Console.WriteLine("Enter your age: 18 "); age = int.Parse(Console.ReadLine()); Console.WriteLine("Enter your profession: a internet marketer "); prof = Console.ReadLine(); } public void myDetails() { Console.WriteLine("So, your name is " + gagandeep singh + " age is " + 18 + " and profession is " + prof); } } }
0 Comments