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

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# Interface

Abstract Classes & Methods

Sealed Classes, Methods

C# Properties

C# Indexer

C# Delegates

C# Generics

C# Collection

System.Object

C#.Net Constructor and Destructor

Constructor is use to initialize object. All objects have a default constructor which is parameterless, however, we can provide our own constructors. In C# constructors are called using new operator. A constructor can also be static, which is executed only once when class is loaded.

Rules to define 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. Parameterized constructors are possible.
5. Constructor can be overloaded like method overloading.
6. Constructor do not have any return type.

Types of Constructor
1. Default Constructor: A constructor without having any parameters called default constructor.
2. Parameterized Constructor: A constructor with at least one parameter is called as parameterized constructor.
3. Constructor Overloading: In c# we can overload constructor by creating multiple constructor with same name and different parameters, like method overloading.
4. Copy Constructor: Copy Constructor is used to copy value of one object into another object.

    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.
Importance 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.

    class A
    {
        static A()
        {
            Console.WriteLine("Static constructor");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            A ob = 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");
    }
    

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 MyClass0
    {
        ~MyClass()
        {
            // destructor code
        }
    }
    

Exercise Questions on Constructor:
  1. Find area of circle and rectangle using constructor overloading.
  2. Find sum of integer number, float number, string value using constructor overloading.
Updated: 06-Feb-19