
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What are the differences between constructors and destructors in C#?
Constructors
A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
A constructor has exactly the same name as that of class and it does not have any return type.
Constructor has the same name as the class name −
class Demo { public Demo() {} }
The following is an example −
Example
using System; namespace LineApplication { class Line { private double length; // Length of a line public Line() { Console.WriteLine("Object is being created"); } public void setLength( double len ) { length = len; } public double getLength() { return length; } static void Main(string[] args) { Line line = new Line(); // set line length line.setLength(6.0); Console.WriteLine("Length of line : {0}", line.getLength()); Console.ReadKey(); } } }
Output
Object is being created Length of line : 6
Destructors
A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope.It can neither return a value nor can it take any parameters.
It has exactly the same name as that of the class with a prefixed tilde (~), for example, our class name is Demo −
public Demo() { // constructor Console.WriteLine("Object is being created"); } ~Demo() { //destructor Console.WriteLine("Object is being deleted"); }
Let us see an example to learn how to work with Destructors in C# −
Example
using System; namespace LineApplication { class Line { private double length; // Length of a line public Line() { // constructor Console.WriteLine("Object is being created"); } ~Line() { //destructor Console.WriteLine("Object is being deleted"); } public void setLength( double len ) { length = len; } public double getLength() { return length; } static void Main(string[] args) { Line line = new Line(); // set line length line.setLength(6.0); Console.WriteLine("Length of line : {0}", line.getLength()); } } }
Output
Object is being created Length of line : 6 Object is being deleted
- Related Articles
- PHP Constructors and Destructors
- C++ Interview questions based on constructors/ Destructors
- What are destructors in C# programs?
- What are the differences between C++ and Java?
- What are the differences between C and Java?
- What are the differences between holography and photography?
- What are the differences between lodash and underscore?
- What are the differences between solvency and liquidity?
- What are the differences between IPO and FPO?
- What are the differences between Flutter and Xamarin?
- What are the differences between Telegram and WhatsApp?
- What are the differences between FDMA and CDMA?
- What are the differences between Antivirus and Antispyware?
- What are the differences between Jumpshare and MiMedia?
- What are the differences between MediaFire and MiMedia?

Advertisements