Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Constructor & Deconstructor in C#

C# Constructor

In C#, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C# has the same name as class or struct.

There can be two types of constructors in C#.

  • Default constructor
  • Parameterized constructor

 

C# Default Constructor

A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.

Let’s see example of the default constructor .

using System;  
public class Employee  {  
    public Employee()  { 
        Console.WriteLine("Default Constructor Invoked");  
    }  
}  
class TestEmployee{  
    public static void Main(string[] args)  {  
        Employee e1 = new Employee();  
        Employee e2 = new Employee();  
    }  
}  

Output:

Default Constructor Invoked 
Default Constructor Invoked

C# Parameterized Constructor

A constructor which has parameters is called parameterized constructor. It is used to provide different values to distinct objects.

Parameterized Constructor is used normally to intialise members of the object.

Below is an example of a Parameterized Constructor.

using System;  
public class Employee  {  
    public int id;   
    public String name;  
    public float salary;  
    public Employee(int i, String n,float s)  {  
        id = i;  
        name = n;  
        salary = s;  
    }  
    public void display()  {  
        Console.WriteLine(id + " " + name+" "+salary);  
    }  
}  
class TestEmployee{  
    public static void Main(string[] args)  {  
        Employee e1 = new Employee(101, "Sonoo", 890000f);  
        Employee e2 = new Employee(102, "Mahesh", 490000f);  
        e1.display();  
        e2.display();  

    }  
}  

C# Destructor

A destructor works opposite to constructor, It destructs the objects of classes. It can be defined only once in a class. Like constructors, it is invoked automatically.

A Destructor has no return type and has exactly the same name as the class name (Including the same case).

It is distinguished apart from a constructor because of the Tilde symbol (~) prefixed to its name.

Note: C# destructor cannot have parameters. Moreover, modifiers can’t be applied on destructors.

Let’s see an example of constructor and destructor in C# which is called automatically.

using System;  
public class Employee  {  
    public Employee()  {  
        Console.WriteLine("Constructor Invoked");  
    }  
    ~Employee()  {  
        Console.WriteLine("Destructor Invoked");  
    }  
}  
class TestEmployee{  
    public static void Main(string[] args)  {  
        Employee e1 = new Employee();  
        Employee e2 = new Employee();  
    }  
}  

Output:

Constructor Invoked
Constructor Invoked
Destructor Invoked
Destructor Invoked

Try the following example in the editor below.

We have a class defined for Student. Create a constructor for the class which takes three parameters name(string), age(int), rollno(int) and assign values to the member variables. Also, create a destructor which prints name, age, rollno each in new line.

Start solving Constructor & Deconstructor in C# on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test