Constructor C#
Constructor is a special method in a class , which is
called automatically when a
Class is instant is created . The constructor is created in
the same name of a class .
A constructor does not have any refund type ,but constructor
can be overloaded.
In .Net , constructor declaration is not must ,, it
automatically create and called by
.Net
public class
myClass
{
public myClass()
{
//This is a
constructor
// TODO: Add
constructor logic here
//
}
}
//I am creating an
instance of a class
//costrutorr
automaticall called
myClass obj = new
myClass();
Static Constructor : The constructor was introduced
for C#. This is a special constructor and gets called before the first object
is created of the class, it is use to instantaneities some static data.It is
access by only static member.Statis constructor do not accept any parameter.
public class
myClass
{
protected static readonly DateTime instanceCreateTiem;
public myClass()
{
//
// TODO: Add
constructor logic here
//
}
//Static constructor
static myClass()
{
instanceCreateTiem = DateTime.Now;
// The following statement
produces the first line of output,
// and the line occurs only
once.
Console.WriteLine("Static constructor sets start time to {0}",
instanceCreateTiem.ToLongTimeString());
}
}
//I am creating an
instance of a class
//costrutorr
automaticall called
myClass obj = new
myClass();
In the console you will get like
Static constructor sets
start time to 12/03/2009 00:10:20:20
Which shows that static constructor works as designed by
Microsoft.
The main use of static constructor to initialized some static
fields to perform some specific purpose only.
Private Constructor : We can declare a constructor private also ,this is a special instance
constructor. Classes that contain static members only are generally used
Private construtor . By setting the access modifier to
private it make clear that the constructor can not be instantiated .Generally in
designed pattern , Singleton pattern
Follow the rule ,
where class constructor is private .This make sure in a whole solution only one
instance of class can be created.
Example 1:
public class
classB
{
public int
A; // Instance field
public classB()
{
//
// TODO: Add
constructor logic here
//
}
private classB() // This is the private constructor
{
this.A = 5;
}
}
Example 2: Designed Patterns
//Create a singleton
class
public sealed
class myClass
{
private static myClass instance = null;
private static readonly object instancelock = new
object();
public myClass() //general constructor
{
}
public static
myClass Instance
{
get
{
lock (instancelock)
{
if (instance == null)
{
instance = new myClass();
}
return instance;
}
}
}
}
//Calling instance of
class
protected void Button1_Click(object
sender, EventArgs e)
{
var instance1 = myClass.Instance;
var instance2 = new myClass();
}
//only one instance of this class is
possible
Copy Constructor : The feature is that , C# allow us to copy constructor .
Example 1
class myclass
{
// Copy constructor.
public myclass(myclass previousPerson)
{
Name = previousPerson.Name;
address = previousPerson.Age;
}
// Instance constructor.
public myclass(string name, int age)
{
Name = name;
address = age;
}
public string
address { get; set;
}
public string
Name { get; set;
}
}
Here myclass is
iniatialised , passing Name and address ,then the constructor is passed to
another constructor is passed during initialization .This behaviors is known as
copy constructor.
Constructors in
Inheritance :
Inheritance is a feature of any object oriented programming
language .A class is created and its child is inherited .The first class is
called Base class and rest child class .When this type of situation arrives and
child class in instantiated , the
Contractor behaves
as followes
Base class
constructor will called first , then child class constructor will be created.
public class
myBase
{
public myBase()
{
Console.WriteLine("I
am now in the Base class");
}
}
public class
myDerived : myBase
{
public myDerived()
{
Console.WriteLine("I am now in the Child class");
}
}
myDerived obj = new
myDerived();
//the output will be
I am now in the Base class
I am now in the Child class
//That shows that base class
contructor called first the child class
No comments:
Post a Comment