ankitweblogic logo
W e b t u t o r i a l s

Console C#.Net Tutorial

Visual Studio.NET IDE

Define C#.NET

C# Comment

C# Variables

C# Data Types

C# Escape Sequence

C# Operators

Exercise-1

Format String

Operator Precedence

C# Keywords

Constant Variable

Type Conversion

Flow Control

Exercise Loops & Nested Loop

C# Arrays

C# Strings

User-Define Methods

Variable Scope

C# Enumerations

C# Structure

C# Exception Handling

Object Oriented Programming

C# Classes

Constructor & Destructor

C# Inheritance

C# Polymorphism

C# Operator Overloading

C# Method Overriding

C# Interface

Abstract Classes & Methods

Sealed Classes, Methods

C# Properties

C# Indexer

C# Delegates

C# Generics

C# Collection

System.Object

C#.Net Constructor and Destructor

In C#.Net Constructor is use to initialize the object. All objects have a default constructor which is parameterless, however, we can provide our own constructor also. In C#.Net, Constructors are called using new keyword.

Rules to define a constructor:

  1. Constructor name is same as the class name
  2. Constructor are always public. However we can create private constructor also but creating instance of this class is not allowed.
  3. Constructor are invoked automatically when the object is created.
  4. Constructor can be parameterized.
  5. Constructor can be overloaded like method overloading.
  6. Constructor do not have any return type.

Types of Constructors:

1. Default Constructor: - A Constructor without having any parameter is known as default constructor.

Example 1: Class with a default constructor.

class example
{
    public example()
    {
        Console.WriteLine("Default constructor called");
    }
}

static void Main(string[] args)
{
    example ex = new example();
    Console.ReadLine();
}

2. Parameterized Constructor: - A Constructor with at least one parameter is called as parameterized constructor.

Example 2: Find Square of a number using parameterized constructor.

class example2
{
	public example2(int a)
	{
		int b = a * a;
		Console.WriteLine("Sq = " + b);
	}
}
	
static void Main(string[] args)
{
	example2 ex2 = new example2(10);
	Console.ReadLine();
}

3. Constructor Overloading: - In C#.Net we can overload constructor by creating multiple constructors that have same name but different parameters, like method overloading.

Example 3: Find area of square and area of rectangle using constructor overloading.

class example3
{
    //area of square
    public example3(int a)
    {
        int b = a * a;
        Console.WriteLine("Sq = " + b);
    }

    //area of rectangle
    public example3(int a, int b)
    {
        int c = a * b;
        Console.WriteLine("Area of rectangle = " + c);
    }
}
static void Main(string[] args)
{
    example3 ex3 = new example3(10);
    example3 ex4 = new example3(10, 20);
    Console.ReadLine();
}

4. Copy Constructor: - Copy Constructor is used to copy values of one object into another object.

Example 4: Copy constructor.

class A
{
	int num;
	public A()
	{
		num = 10 ;
	}
	public A( A xyz)
	{
		num = xyz.num;
	}
	public void display()
	{
		Console.WriteLine(num);
	}
}
class Program
{
	static void Main(string[] args)
	{
		A ob = new A();
		A ob2 = ob;
		ob.display();
		ob2.display();
		Console.Read();
	}
}

5. Static Constructor: - When we declared constructor as static it will be invoked only once for any number of instances of the class. Static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.

Important points of static constructor:
Static constructor will not accept any parameters because it is automatically called by CLR.
Static constructor will not have any access modifiers.
Only one static constructor will allowed.

Example 5: Static constructor.

class A
{
    static A()
    {
        Console.WriteLine("Static constructor called");
    }
}
class Program
{
    static void Main(string[] args)
    {
        A ob1 = new A();
        A ob2 = new A();
        A ob3 = new A();
        Console.Read();
    }
}

6. Private Constructor: - Private constructor contains static member only. If a class has one or more private constructor and no public constructor, then other classes is not allowed to create instance of this class this mean we can neither create the object of the class nor it can be inherit by other class. The main purpose of creating private constructor is to restrict the class from being instantiated.

private Sample() // Private Constructor Declaration
{
	Console.WriteLine("Private Constructor with no prameters");
}

C# Destructor

Destructor are used to clean up the memory used by objects after objects are no longer required. The code in the destructor will be executed when garbage collection occurs. In general we don't have to provide code for destructor, instead default operation works for us. However we can specify destructors in our code.

Rules to declare Destructors:

  1. Destructor have the same name as class name.
  2. No access modifier can be used with it.
  3. A class can have only one destructor.
  4. Destructor cannot be inherited or overloaded.
  5. Destructor do not have any return type. Not even void.
Class MyClass
{
	~MyClass()
	{
		// destructor code
	}
}

Exercise Questions on Constructor:

  1. Find area of circle, rectangle and triangle using constructor overloading.
  2. Find sum of integer number, float number, string value using constructor overloading.
Updated: 18-Jun-19