In C# programming, the method destructor is used in class for destroying the instances of that particular class. It will destroy those instances of the class that we don’t need later. The destructor name and the class name are the same. We use a destructor in a class, and a class contains only one destructor. We can’t inherit or overload a destructor in C# programming. There is no need to call the destructor as it will be invoked automatically. When our C# program exists, then the destructor is called automatically. We have different examples to understand this method through practical demonstration of examples.
Example # 1: Using Destructor in C# Programming in Ubuntu 20.04
In this example, we use this destructor method in our C# program in Ubuntu 20.04. Remember one thing; when you write this code on the text editor of Ubuntu 20.04, you must save this file with the “.cs” extension. Let’s see how this destructor method works in the C# program.
We start our C# program with the “using System” statement. The using statement will allow you to specify many resources in one statement. Here, we have “using System”, which means we use the system library in our code. It will provide all the functions and classes we need in this code. We also have a namespace having the name “Destructor” here. We use the keyword “namespace” for organizing our C# code.
Now, we have our main class named “Program”. Inside this class, we are creating a constructor class with the name “Program()”. The “public Program()” is our constructor here. In this constructor, we have only one “Console.WriteLine” statement. We may use many statements inside this constructor. The “Console.WriteLine” is used when displaying some information or message on the output screen. Here, we use it to display “We call a destructor” on the screen.
After this, we are going to create our destructor class with the same name as the constructor class. We put the “~” sign at the start of the destructor class. The destructor class is named “~Program ()” here. Then, we use the “main” function outside the destructor class, which is written as “public static void Main (String  args)”. Here, we see that it is “public” and “static”, and “(String  args)” is used so that our command-line values are in it. Then, we have this “new Program” statement in this main function; the “new” is a keyword that we use to create an object of the class. It will display the data of the constructor class first, then the destructor class called automatically, and also display the data of the destructor class as well. The destructor class will destroy the instances of the class when it ends and no longer need these instances.
We can get the output of our C# code with the help of commands, which we run on the terminal of Ubuntu 20.04. First, we have to compile our code using the “mcs” command with the filename having an extension of “.cs”. Then, we have to execute this compiled program using the “mono” command with the same filename having an extension of “.exe”. Remember that we use the “.cs” file extension with the “mcs” command and the “.exe” file extension when using the “mono” command.
This output shows that the statement inside the constructor class is displayed, and then the destructor class is automatically called. The statement of this destructor class is also displayed, and the destructor class destroys the instance of the constructor class.
Example # 2:
Now, we will explore another example to better understand the C# destructor concept. Look at this example, and how it works:
This code also contains the “using System” and “namespace”, which we discussed in detail in our previous code. The name of the “namespace” is “CSharp” here. Then, we have a class named “Person”. This is our constructor class. We are declaring a “string” in this constructor class with the name “name”. After this, we invoked the method “void getname()”. The “getname()” method is used for returning the name of the string data type. Inside this method, we have a “ConsoleWriteLine()” statement. This statement will print the data written inside the brackets of this “Console.WriteLine()”. Now, we also have a destructor class inside this constructor class with the same name as the constructor class. The “~ Person()” is the destructor class here.
After this, we create a “Console.WriteLine()” statement in this destructor class. Next, we are invoking a method below this destructor class, which is “public static void Main (String  args)”. We discussed this method in detail in the previous code. Now, move forward to the next line in which we create an object of the “Person” class with the name “pr”. Here, “new” is a keyword used for creating an object. In the next line, we have “pr.name”, the “pr” is the object, and the name is the string type variable that we declare at the start of the program. Next, we use “string name” here. We assign a name to this, which is “Robert Liam”, and we call the “getName()” method with the object of the class “pr”.
The output of our destructor code is here in this image. Look at this output and how it displays data of our code.
You can see this when we enter the name in the string and call the method of “getName()”. It returns the name using the first “Console.WriteLine” statement, which is written inside this “getName()” method. Then, the destructor is called automatically, and it destroys the constructor class instances.
Example # 3:
Here, we are exploring another interesting example for you:
The “namespace” is named “MyCsharpProgram” here, and the class is called “Number”, which is the main class. The elements of this class are private by default. We are declaring “realNum” and “imgNum” in this class which is of the “int” data type. After this, we will define the constructor class with the name “Number()” and initialize these previous integers here.
Then, we have a setValue method used to set the values of “realNum” and “imgNum”. We assign “Rnum” to “realNum” and “Inum” to “imgNum” inside this method. We are creating another method named “DisplayValue”, which will display the values of both integers using the “Console.WriteLine()” statement. After that, we create a destructor class outside this method.
The “~Number()” is the destructor class here. We also have a “Console.WriteLine()” inside this destructor class. Then, we have a driver class named “Program”. We create a “main” method and then create an object “N” of class “Number” with the help of the “new” keyword. This object “N” invokes the constructor. Then, we call the “setValue()” method and pass values to this method. In the next line, the “DisplayValue()” method is called, which will display the values of “realNum” and “imgNum”.
When the constructor is called, the methods inside the constructor are executed in the output. The “setValue” method sets the values of both “realNum” and “imgNum”, then the “displayMethod” displays the values of both numbers. Here, the destructor destroys these objects as they are not needed later and prints the statement written inside the destructor. In other words, the destructor is called here automatically.
In this guide, we explore the concept of destructor in C# programming in Ubuntu 20.04. We come to know that the destructor in C# programming is used for the destruction of the instances of the constructor class when we don’t need those instances later. We explain this destructor concept in detail. And also, provide different examples with their output for you so that you try these examples yourself and get the idea about this concept. We explain every line of this C# code in detail for your understanding. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.