- Constructor is a member method of a class which is invoked automatically when an object to the class is created.
- Constructor name should be same as the name of the class name.
- Constructor is used to initialize values in to the data fields of the class or to pass required values in to the data fields.
- Default Constructor
- Default Constructor
- User Defined Defined Constructor
- Parameterized Constructor
- Copy Constructor
- Private Constructor
- Static Constructor
If there is no constructor defined with in the class then, system will create its own constructor and will assign default values into the data fields.
when an object is created to the class first system will search for a constructor with in the class, If there is no constructor with in the class, system will create its own constructor and will assign the default values.
User Defined Default Constructor:
Parameterized Constructor:
A Parameterized Constructor accepts arguments to store the values in to the data fields. Using this constructor we can store different set of values into different objects created to class.
Example for Parameterized Constructor:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConstructorExamples
{
class StudentDetails
{
int sid, smarks;
string sname;
public StudentDetails(int id,int marks,string name)
{
this.sid = id;
this.sname = name;
this.smarks = marks;
}
public StudentDetails(int id, string name)
{
this.sid = id;
this.sname = name;
}
public void Display()
{
Console.WriteLine("Student id : " + sid);
Console.WriteLine("Student name : " + sname);
Console.WriteLine("Student marks : " + smarks);
}
}
class ParameterizedConstructor
{
static void Main(string[] args)
{
StudentDetails sd1 = new StudentDetails(589,99,"ram");
StudentDetails sd2 = new StudentDetails(590,86,"ravi");
StudentDetails sd3 = new StudentDetails(590,"krishna");
sd1.Display();
sd2.Display();
sd3.Display();
Console.Read();
}
}
}
User Defined Default Constructor:
- User Defined Default Constructor is created by the programmer.
- This constructor doesn't accept any arguments.
- Generally this constructor is used to initialize required values in to the data fields.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConstructorExamples
{
class Student1
{
int sid, smarks;
string sname;
public Student1()
{
this.sid = 590;
this.sname = "Ram";
this.smarks = 99;
}
public void Display()
{
Console.WriteLine("Student id : " + sid);
Console.WriteLine("Student name : " + sname);
Console.WriteLine("Student marks : " + smarks);
}
}
class UDConstructor
{
static void Main(string[] args)
{
Student1 s1 = new Student1();
s1.Display();
Console.Read();
}
}
}
Parameterized Constructor:
A Parameterized Constructor accepts arguments to store the values in to the data fields. Using this constructor we can store different set of values into different objects created to class.
Example for Parameterized Constructor:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConstructorExamples
{
class StudentDetails
{
int sid, smarks;
string sname;
public StudentDetails(int id,int marks,string name)
{
this.sid = id;
this.sname = name;
this.smarks = marks;
}
public StudentDetails(int id, string name)
{
this.sid = id;
this.sname = name;
}
public void Display()
{
Console.WriteLine("Student id : " + sid);
Console.WriteLine("Student name : " + sname);
Console.WriteLine("Student marks : " + smarks);
}
}
class ParameterizedConstructor
{
static void Main(string[] args)
{
StudentDetails sd1 = new StudentDetails(589,99,"ram");
StudentDetails sd2 = new StudentDetails(590,86,"ravi");
StudentDetails sd3 = new StudentDetails(590,"krishna");
sd1.Display();
sd2.Display();
sd3.Display();
Console.Read();
}
}
}
From the above program we can note that a class can contain more than one constructor and these constructors can be overloaded for different arguments.
Copy Constructor:
- This constructor is used to copy the data of an existing object in to newly created object.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConstructorExamples
{
class Student3
{
public int sid, smarks;
public string sname;
public Student3()
{
Console.WriteLine("Enter Student Details ...");
this.sid = Convert.ToInt32(Console.ReadLine());
this.sname = Console.ReadLine();
this.smarks = Convert.ToInt32(Console.ReadLine());
}
public Student3(Student3 ObjTemp)
{
this.sid = ObjTemp.sid;
this.sname = ObjTemp.sname;
this.smarks = ObjTemp.smarks;
}
public void Display()
{
Console.WriteLine("Student id : " + this.sid);
Console.WriteLine("Student name : " + this.sname);
Console.WriteLine("Student marks : " + this.smarks);
}
}
class CpyConstructor
{
static void Main(string[] args)
{
Student3 s1 = new Student3();
Student3 s2 = new Student3(s1);
s1.Display();
s2.Display();
Console.Read();
}
}
}
Static Constructor:
- Static constructor is used to initialize static data fields.
- A class can contain only one static constructor.
- Static Constructor will not allow any parameters.
- There should be no access modifier in static constructor definition.
- Static constructor cannot initialize non-static data fields, but non-static constructor can initialize static variables.
- Static constructor is called when the class is called for first time.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConstructorExamples
{
class StaticConstructor
{
public static int x;
public int y;
public StaticConstructor()
{
y = 20;
}
static StaticConstructor()
{
x = 10;
}
public void display()
{
Console.WriteLine("Vaule of y : " + y);
y++;
Console.WriteLine("Vaule of x : " + x);
x++;
}
}
class StaticConstructorMain
{
public static void Main()
{
StaticConstructor sc1 = new StaticConstructor();
sc1.display();
StaticConstructor sc2 = new StaticConstructor();
sc2.display();
StaticConstructor sc3 = new StaticConstructor();
sc3.display();
Console.Read();
}
}
}
Private Constructor:
- Constructor whose accessibility is private is known as private constructor.
- If a class contains private constructor then we cannot create an object for the class outside the class.
- Generally private constructors are used in Remoting concept.
No comments:
Post a Comment