C# tutorial- constructor

Constructor


A Constructor is a special method that can be used to initialize objects of the class when they are created. In the example below, when a student object is created, the empty strings ("") are initialized to its variable members, stnumber, stname, and stsex.

Example:

class Student

    {

        private string stnumber;

        private string stname;

        private string stsex;

 

        public Student(){ //Constructor

          stnumber="";

          stname="";

          stsex="";

}

        public void setnumber()

        {

            Console.WriteLine("Enter student's number:");

            stnumber =Console.ReadLine();

        }

 

        public void getnumber()

        {

            Console.WriteLine("Student's number is:{0}",stnumber);

        }

        public string stunumber{ //stunumber property

            set { stnumber=value;}

            get { return stnumber; }

 

    }

 

 }

 

A constructor can accept parameters as you see in the following example. By using parameters, you can pass values to the object. So you can initialize any values to the variables of the class.

Example:

class Student

    {

        private string stnumber;

        private string stname;

        private string stsex;

 

        public Student(){ //Constructor

          stnumber="";

          stname="";

          stsex="";

}

 

//Constructor with parameters

  public Student(string id, string name, string sex){

          stnumber=id;

          stname=name;

          stsex=sex;

}

 

        public void setnumber()

        {

            Console.WriteLine("Enter student's number:");

            stnumber =Console.ReadLine();

        }

 

        public void getnumber()

        {

            Console.WriteLine("Student's number is:{0}",stnumber);

        }

        public string stunumber{ //stunumber property

            set { stnumber=value;}

            get { return stnumber; }

 

    }

 

 }

 

To call the constructor with the parameters you need to supply values of the parameters when you create an object.

Student stu = new Student("C001","Vibol Sothy","M");





Comments

Name:
Comment:
Enter the text:
CAPTCHA image

....................................................................................................................Home | Forum | About | Contact
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like