Constructors in C#
Introduction
A
constructor is one that has special method which runs automatically when we
create instance of the class. Please note following points here which is
applicable:
(i)
Constructors has same name as the class.
(ii)
Constructors take parameters.
(iii)
Constructors never returns any value not even void.
(iv)
Every class must have a constructor, if we don’t write constructor, compiler
generates a default one which doesn't actually do anything.
(vi)
Default constructor has public accessibility.
//Example
of constructor
class ClassRoom
{
private int boycount; //field
public ClassRoom() //default
constructor
{
boycount = 30;
}
public double Avg() //method
{
//statements goes here
}
}
In above example, constructor is marked as public. If we
omit this keyword, the constructor will be private and it will have property
like methods and fields. If we put constructor as private then obviously,
cannot be used outside the class, which will prevent from being able to create ClassRoom
objects from methods that are not part of the ClassRoom class. Sometimes we
will get some exception here. We might therefore think that private
constructors are not that valuable, never, they do have their uses. Don't get
confused here too, to check this, change any constructor to private and look at
the result.
Now, to use ClassRoom class and its Area method, dot
notation will be used to invoke the Area method on a ClassRoom object:
ClassRoom r; //creating a class variable, object
r = new ClassRoom(); //initializing class variable
double newAvg = r.Avg(); //invoking class method
In above example, we have created a new class variable
named r and in just next line we are initializing all components of ClassRoom class
to new r class variable. After this, we are invoking the class method by using
dot(.) operator and assigning the value in newAvg.
Constructors
Overloading
Till
now we discussed on basics of contractors, let's have some discussion on its
overloading techniques. In above example, ClassRoom objects will always be 30
because the default constructor sets the boycount to 30 and it stays at 30; the
boycount field is private and there is no easy way of changing its value after
it has been initialized once. However, we should realize that a constructor is
just a special kind of method and that's it. We knew that methods can be
overloaded and constructor too. Constructor overloading is a type of Static
Polymorphism. Using constructor overloading, we can define any number of
constructors to the same class. But ensure that each constructor has a
different number and type of parameters defined.
//Example
of constructor overloading
class ClassRoom
{
private int boycount; //field
public ClassRoom() //default
constructor
{
boycount = 30;
}
public ClassRoom(int bcount)
//overloaded constructor
{
boycount = bcount;
}
public double Avg() //method
{
//statements goes here
}
}
In
above example, we have created a new constructor having one parameter and we
are assigning parameter value in field.
Now,
to use above new constructor we need to pass the parameter value, look at
example:
ClassRoom
r;
r
= new ClassRoom(33);
Now compiler will decide which constructor it should
call based on the parameters that we have passed. In above example we have
passed an integer value that is 33. Please note the order of defined
constructors can be anything, like in above class example, we have two
different constructors and anyone may be first.
Comments
Post a Comment