Click here to Skip to main content
15,898,874 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Please tell the use of constructor...
Posted

Constructor:
The class name and the method name are same. When you create an object for the class the method will be called directly. You don't want to call that method separately using that object.
Example:
C#
Class Praveen
{
public praveen
{

} 
}
class main
{
static void main()
{
praveen p=new praveen();  // the method will be called directly when you create the object.
}
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 23-Jun-12 5:00am    
Please use <pre> tags around code snippets.
See here[^] for full details of classes.
 
Share this answer
 
Simply the Constructor is a function that will be called when you define an object of the class...
it mostly used for initiate data members and set default values.

Example:
class Student
{
    private int Id;
    Private String Name;

    private Student()
    { 
       Id = 0;
       Name = "ABC";
    }
}


So now when ever you create a Student Object it will take the default values.
Regards
 
Share this answer
 
look this very good Article[^] on constructor with FAQ.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900