A constructor is a special method of a class or
structure in object-oriented programming that initializes an object of that
type. A constructor is an instance method that usually
has the same name as the class, and can be used to set the values of the
members of an object, either to default or to user-defined values.
Some key point of constructor
·
Constructor is member of a class and structure.
·
Constructor is a special type of function which
will be fire at time of object is creating or class is load.
·
Constructor name is always same name as class
name.
·
Constructor not contains any return type, because
it not returns any value.
·
Constructor can contain a parameter or not.
Syntax of constructor
<Access modifier><class name (type><parameter>)>
{
Code initialization
}
Instance constructor:-While define a
constructor without using static key word is known as instance constructor.
Key point
·
Only instance constructor can hold parameter
·
It initialize at the time of object is created.
·
In a single class multiple instance constructors can
be created but condition is each constructor should be unique to other by using
different signature and number of parameter should vary.
Static constructor
When a constructor is created as
static, it will be invoked only once for all of instances of the class and it are
invoked during the creation of the first instance of the class or the first
reference to a static member in the class. A static constructor is used to
initialize static fields of the class and to write the code that needs to be
executed only once.
Key Point constructor
Ø A static constructor does
not take access modifiers or have parameters.
Ø A static constructor is
called automatically to initialize the class before the first instance is
created or any static members are referenced.
Ø A static constructor cannot
be called directly.
A default constructor is a constructor which
can be called with no arguments /with
any parameter.
Key point
Ø Definition of the
constructor outside the class body.
Ø Inhibiting the automatic
generation of a default constructor by the compiler.
Ø Explicitly forcing the
automatic generation of a default constructor by the compiler.
using System;
namespace DefaultConstractor
{
class demo
{
int a, b; // class variable declartion
namespace DefaultConstractor
{
class demo
{
int a, b; // class variable declartion
public addition() //without any parameter or argument
{
a = 100;
b = 175;
}
public static void Main()
{
addition
obj = new addition();
Console.WriteLine(obj.a);
Console.WriteLine(obj.b);
Console.Read();
}
}
}
Parameterized
Constructor:-
A constructor with at least one parameter is called parameterized constructor. The
advantage of a parameterized constructor is that you can initialize
each instance of the class to different values.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
parameterizedConstuctor
{
class Course
{
ulong cid;
string cname;
double cfees;
internal Course(ulong CourseID, string courseName, double courseFees)
{
cid = CourseID;
cname =
courseName;
cfees =
courseFees;
}
internal void
DisplaycourseDetails()
{
Console.WriteLine("Course Code : " + cid);
Console.WriteLine("Couse Name : " + cname);
//Console.WriteLine("Couse Name
: " + string.Format("{0:0.00}"+ cfees));
// string s =
string.Format("Course Fees is {0:0.00}", +cfees);
// Console.WriteLine(s);
Console.WriteLine( string.Format("Course
Fees is {0:0.00}",
+cfees));
}
static void Main(string[] args)
{
Course obj1 = new Course(101, "C# .net", 1100.00);
obj1.DisplaycourseDetails();
Course obj2 = new Course(102, "Angular", 7000.00);
obj2.DisplaycourseDetails();
Course obj3 = new Course(103, "MVC", 1100.00);
obj3.DisplaycourseDetails();
Console.ReadLine();
}
}
}