Understanding
Constructors in C#
Constructors
are used for initializing the members of a class whenever an object is created
with the default values for initialization.
If a class is not defined with the
constructor then the CLR (Common Language Runtime) will provide an implicit
constructor which is called as Default Constructor.
A
class can have any number of constructors provided they vary with the number
of arguments that are passed, which is they should have different signatures.
-
Constructors do not return a value.
-
Constructors can be overloaded.
-
If a class is defined with static and Non-static constructors then the privilege
will be given to the Non-static constructors.
The following are the access modifiers for constructors,
Public
: A constructor that is defined as public will be called whenever a class
is instantiated.
Protected
: A constructor is defined as protected in such cases where the base class
will initialize on its own whenever derived types of it are created.
Private
: A constructor is defined as private in such cases whenever a class which
contains only static members has to be accessed will avoid the creation of the
object for the class.
Internal
: An internal constructor can be used to limit concrete implementations of
the abstract class to the assembly defining the class. A class containing an
internal constructor cannot be instantiated outside of the assembly.
External
: When a constructor is declared using an extern modifier, the constructor
is said to be an external constructor.
Types
of Constructors
i)
Static : Used for initializing only the static members of the class.
These will be invoked for the very first time the class is being loaded on the
memory. They cannot accept any arguments. Static Constructors cannot have any
access modifiers.
Syntax
----
Static ClassName()
{
//Initialization
statements;
}
ii)
Non-Static : are used for initializing the Non-Static and Static members
of a class. These will be invoked everytime a new object is defined for a
class.
Syntax
---
Public ClassName([argsInfo])
{
//Initialization
statements;
}
Using
System;
Class
SampleConstructor
{
public
static int s;
public
int ns;
static SampleConstructor()
{
s
= 10;
//ns
= 20; --- Error cannot be assigned like this
}
public SampleConstructor()
{
ns=100;
s=200;
}
}
Class
UseSampleConstructor
{
public
static void Main()
{
SampleConstructor
sc = new SampleConstructor();
Console.WriteLine(“{0},{1}â€,sc.s,
SampleConstructor.s, sc.ns);
}
l
}
l
Error(Cannot
call like this)
If
you observe in the above example the static variable `ns' cannot be assigned a
value in the static Constructor as it is a Non-static member of the class and
a static constructor cannot initialize a Non-Static member.
Also
as you see in the above code the Non-static constructor initializing the
values for both the static and Non-Static members of class, when we say `sc'
we are creating an object for the class and using the object we do the call
the methods, it is not valid to call a static member of class using the object
of the class we have to call it through the method name/Constructor name
through the member will be invoked.