By using constructors we can initialize the objects data
Constructors should not contain return type
Constructors should be same as class name.
By using constructors we can initialize data to the fields or global variables
Types of constructor :
Constructors can be classified into 5 types
- Default Constructor
- Parameterized Constructor
- Copy Constructor
- Static Constructor
- Private Constructor
1.Default Constructor :
A constructor without any parameters is called as default constructor.
Drawback of default constructor is every instance of the class will be
initialized to same values and it is not possible to initialize each
instance of the class to different values.
2.Parameterized Constructor :
A constructor with at least one parameter is called as parameterized
constructor. Advantage of parameterized constructor is you can
initialize each instance of the class to different values.
3.Copy Constructor :
A parameterized constructor that contains a parameter of same class
type is called as copy constructor. Main purpose of copy constructor is
to initialize new instance to the values of an existing instance.
4.Static Constructor :
You can create a constructor as static and when a constructor is
created as static, it will be invoked only once for any number of
instances of the class and it is during the creation of first instance
of the class or the first reference to a static member in the class.
Static constructor is used to initialize static fields of the class and
to write the code that needs to be executed only once.
5.Private Constructor : You can also create a constructor as private. When a class contains at
least one private constructor, then it is not possible to create an
instance for the class. Private constructor is used to restrict the
class from being instantiated when it contains every member as static.
Sample code:
public partial class _Default:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
Student s1=new Student();
Student s2=new Student();
{
protected void Page_Load(object sender,EventArgs e)
{
Student s1=new Student();
Student s2=new Student();
}
}
public class Student
{
public int id_no=2212;
public string name="Bill";
}
}
public class Student
{
public int id_no=2212;
public string name="Bill";
}
}
Note:
->In the above program we have created 2 objects for the given Student class,
so with duplicate data 2 objects will be created in the memory
->To solve this problem refer to the below modified Student class
Modifed code:
public partial class _Default:System.Web.UI.Page { protected void Page_Load(object sender,EventArgs e) { Student s1=new Student(2212,"Bill"); Student s2=new Student(2213,"Steve"); }
} public class Student { public int id_no; public string name; public Student(int id,string nm) { id_no=id; name=nm; } }
Explanation:
->Here during execution of this line<b>
Student s1=new Student(2212,"Bill")
-> CLR(Common Language runtime) will understand that it has to create an object of Student class (new Student) and during execution of the next portion ->Student(2212,"Bill")it will call Student class Constructor which will assign data to the class level variables ->CLR will follow the same process for this also<b> </b>line too<b> Student s1=new Student(2213,"Steve"). ->Here 2 objects will be created in memory but with different data,whereas each student is having separate identity.
No comments:
Post a Comment