Constructor in Console Application



In previous .net post we have already learned about create console application and namespace in console application c# .net.

In this console application c# tutorials we will understand what is constructor, types of constructor and features of constructor in detail.
Before creating a c# example, we should have a clear idea about constructor and class.

What is constructor in C# .net

Constructor is a special method or function of class having no return type and same name as class name.

– Constructor with no return type.
– Constructor name is same as class name.
– If we do not create constructor for class, the compiler will create a default constructor internally.
– The constructor with no argument and no body known as default constructor.
– Constructor with argument is known as parameterized constructor.
– All constructor by default public, we can also create private constructor.
– Static constructor automatically call at the time of class loading.
– We can only one static constructor can create in a class.

The class can have any number of constructor and constructor do not have return type. The constructor name must be same as class name.

here we create a constructor the constructor method name same as class name shows in below example.

Example of constructor in c#

class ConstExample
{
public ConstExample()
{
Console.WriteLine("Exmaple of constructor");
}
}

 Types of constructor

– Default Constructor
– Parameterized Constructor
– Cpopy Constructor
– Static Constructor
– Private Constructor


1. Default Constructor

– A constructor without any parameters called default constructor. We can also say this type of constructor does not take any parameters.
Default constructor all instance of the class will be initialized without take any parameter value.

Example code of default constructor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConstExample
{
class DefExample
{
public int a, b;
public DefExample() // Default Constructor
{

a = 120;
b = 200;
}

}
class Program
{
static void Main(string[] args)
{
DefExample obj = new DefExample(); // object of class created the constructor will automatically called
Console.WriteLine(obj.a);
Console.WriteLine(obj.b);
Console.ReadLine();
}
}
}

The output of default constructor example is :

120
200


2. Parameterized Constructor

– A constructor with at least one parameter is called parameterized constructor. In this type of constructor we can initialize each instance of class to different values.

Example of parameterized constructor in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConstExample
{
class ParaExample
{
public int sum;
public ParaExample(int a, int b) // Declaring paramiterrized constructor with int a and b 
{
sum = a + b;
}

}
class Program
{
static void Main(string[] args)
{
ParaExample obj = new ParaExample(10,15); // Paramiterized constructor called
Console.WriteLine("The sum of 10 and 15 = "+ obj.sum);

Console.ReadLine();
}
}
}

The output of parameterized constructor example is :

The sum  of 10 and 15 = 25


3. Copy Constructor

– A Parameterized constructor that contain a parameter of same class type is called copy constructor. Main use of this constructor is to initialize new instance to the values of an existing instance.

Example of copy constructor in c# .net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConstExample
{
class CopyExample
{
public int sum;
public CopyExample(int a, int b) 
{
sum = a + b;
}
public CopyExample(CopyExample m) //Copy Constructor
{
sum = m.sum;

}

}
class Program
{
static void Main(string[] args)
{
CopyExample obj = new CopyExample(10, 15); // create instance of class CopyExample
CopyExample obj1 = new CopyExample(obj); // Copy constructor called , obj detail copied to obj1
Console.WriteLine("The sum of 10 and 15 = "+ obj1.sum);

Console.ReadLine();
}
}
}

The output of copy constructor example is :

The sum of 10 and 15 = 25


4. Static Constructor

A static constructor has the same name as the class name preceded with the static keyword.
A static constructor will execute automatically whenever class of instance is created.
A static constructor will not have any access modifiers. Static constructor will not return anything.
Static constructor will call at the time of class loading. Static constructor will not allow overloading, there is no parameterized static constructor.

Example of static constructor in console application c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConstExample
{
class StaticExample
{

static StaticExample() 
{
Console.WriteLine("Example of Static Constructor");
}
public StaticExample(int a)
{

Console.WriteLine("Example of Instance Constructor = "+ a);
}
}
class Program
{
static void Main(string[] args)
{

StaticExample obj = new StaticExample(10);//this time sstatic and intance both cnstructor are invoked first time

StaticExample obj1 = new StaticExample(10); //only instance constructor will be invojed
Console.ReadLine();
}
}
}

The output of static constructor example is :

Example of Static Constructor
Example of Instance Constructor = 10
Example of Instance Constructor = 10


5. Private Constructor

We can also create a constructor as private. If a class contain at least one private constructor, then it is not possible to create an instance for this class.
Private constructor is used in a class that contain static member only.Private constructor is used to restrict the class from being instantiated when it contain every member as static.

Example of Private Constructor in console application c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConstExample
{
class PrivateExample
{
public int sum;
private PrivateExample() //Private construction declaration
{
Console.WriteLine("Example of Private Constructor");
}
public PrivateExample(int a,int b)
{

sum = a + b;
}
}
class Program
{
static void Main(string[] args)
{

PrivateExample obj = new PrivateExample(10,15);
Console.WriteLine("The sum of 10 and 15 = " + obj.sum);
Console.ReadLine();
}
}
}

The output of private constructor example is :

The sum of 10 and 15 = 25

Related ASP.Net Topics :

Namespaces in Console Application
Console Application Example


Subscribe us

If you liked this asp.net console application post, then please subscribe to our YouTube Channel for more asp.net video tutorials.

We hope that this asp.net post helped you to understand about what is constructor and type of constructor in C#.


Next, asp.net tutorial we will understand about Destructor in C#.


Leave a Reply

Your email address will not be published. Required fields are marked *