Here First I will show you how to Create a constructor.
Create a Constructor:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Constructor1 { class Program { public Program() //create a constructor with the same name of the class. { Console.WriteLine("This is constructor"); }
static void Main(string[] args) { Program p = new Program();//create a object when the constructor is called.
} } }
Output:This is constructor
How to Overload Constructor
Overload a Constructor
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace consructoroverloading { class man //class { private string name; //declare variable private int age;
public man(string theName, int theAge)//create a first constructor { name = theName; age = theAge; Console.WriteLine("man:{0} {1}", theName, theAge); } public man(string theName)//create a second constructor { name = theName; Console.WriteLine("man:{0}", theName); } public man(int theAge)//create a third constructor { age = theAge; Console.WriteLine("man:{0}",theAge); } static void Main(string[] args) { //create a different object for three consructor and pass the value man m1 = new man("m1", 22); man m2 = new man("m2"); man m3 = new man(23);
} } }
Output: man:m1 22 man:m2 man:23
Attachmentssource code (5240-17612-Constructor.rar)
|
No feedbacks found. Be the first to respond...
|