Constructors
Every field(int, string, bool) inisintialized in the constructor. When we say every value types is defaulted to a value, that default value is provided in the constructor. When you do not give the constructor, .net gives a defaultc constructors and assign a default value.
Class Test
{
int i; String j;
Public test()
{
i=20; j=null;
};//implicit constructor
}
Implicitly defined constructor are public by default.
Types of constructor
------------
1. Default (or parameterless constructor)
2. Parameterised Constructor
3. Copy Construtcor
4. Static constructor
Copy Constructor:
--------
class CopyDemo
{
int x;
public CopyDemo(int i)
{
x = i;
}
public CopyDemo(CopyDemo cd) // this is copy constructor
{
x = cd.x;
}
}
---
copyDemo c1=new copyDemo(10);
copyDemo c2=new copyDemo(cd1);
Static Constructor
-------
class Test
{
static Test() //implicitly defined only if we have static fields in class. No need to define it yourself.
{
}
public Test() // explicitly defined.
{
}
}
If a class contains any static variables, then only implicit static constructor will be present. else we will have to define it manually.
---------------------
Why is constructor needed?
Ans-> To intiliaze variable of a class with same value even if we create multiple instances of the class.
A a = new A();
A b = new A();
A c = new A();
i.e. 3 different memoryallocations having same value
class B
{
public int x;
public B(int a)
{
this.X=a;
}
}
Now if we create 3 instances, --todo
Static Vs Non- Static Constructor
---------------------------------
Static fields are intialized by static constructor
Non-Static fields are intialized by non-static constructor
Static constructor executes immediately only once when the execution of the class starts. It is the first block of code to be executed in a class.
Non Static constructor executes once and that too only after creating the instance of the class and it is called every single time a new object is created.
VVIMp:. Static constructor is called only once in a lifetime of a class.
Every single class except a static class constains an implicit non-static constructor if an explicit constructor is not defined.
Static constructor are implicitly defined in a non-static class if the class contains static fields.
Every field(int, string, bool) inisintialized in the constructor. When we say every value types is defaulted to a value, that default value is provided in the constructor. When you do not give the constructor, .net gives a defaultc constructors and assign a default value.
Class Test
{
int i; String j;
Public test()
{
i=20; j=null;
};//implicit constructor
}
Implicitly defined constructor are public by default.
Types of constructor
------------
1. Default (or parameterless constructor)
2. Parameterised Constructor
3. Copy Construtcor
4. Static constructor
Copy Constructor:
--------
class CopyDemo
{
int x;
public CopyDemo(int i)
{
x = i;
}
public CopyDemo(CopyDemo cd) // this is copy constructor
{
x = cd.x;
}
}
---
copyDemo c1=new copyDemo(10);
copyDemo c2=new copyDemo(cd1);
Static Constructor
-------
class Test
{
static Test() //implicitly defined only if we have static fields in class. No need to define it yourself.
{
}
public Test() // explicitly defined.
{
}
}
If a class contains any static variables, then only implicit static constructor will be present. else we will have to define it manually.
VVImp-> Static constructor are implicitly called. They are called as soon as program execution begins. Non-Static constructors are called when a object has been declared on the class(via new object() call). Static constructor are called even if no object of that class if created.
Static constructors can not be overloaded or parameterised. This is because they are called implicitly, and hence, we never have a chance to manually call the static constructor.---------------------
Why is constructor needed?
Ans-> To intiliaze variable of a class with same value even if we create multiple instances of the class.
A a = new A();
A b = new A();
A c = new A();
i.e. 3 different memoryallocations having same value
class B
{
public int x;
public B(int a)
{
this.X=a;
}
}
Now if we create 3 instances, --todo
Static Vs Non- Static Constructor
---------------------------------
Static fields are intialized by static constructor
Non-Static fields are intialized by non-static constructor
Static constructor executes immediately only once when the execution of the class starts. It is the first block of code to be executed in a class.
Non Static constructor executes once and that too only after creating the instance of the class and it is called every single time a new object is created.
VVIMp:. Static constructor is called only once in a lifetime of a class.
Every single class except a static class constains an implicit non-static constructor if an explicit constructor is not defined.
Static constructor are implicitly defined in a non-static class if the class contains static fields.