Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - Destructor in C# with Example

Sep 29, 2013
Introduction:

Here I will explain what is destructor in c# with example, use of destructor in c#.net. Destructor in c# is a special method of a class which will invoke automatically when an instance of the class is destroyed. Destructor is used to write a code that needs to be executed while an instance is destroyed.

Description:


To create destructor we need to create method in a class with same name as class preceded with ~ operator.

Syntax of Destructor


class SampleA
{
public SampleA()
{
// Constructor
}
~SampleA()
{
// Destructor
}
}
Example of Destructor

In below example I created a class with one constructor and one destructor. An instance of class is created within a main function. As the instance is created within the function, it will be local to the function and its life time will be expired immediately after execution of the function was completed.


using System;
namespace ConsoleApplication3
{
class SampleA
{
// Constructor
public SampleA()
{
Console.WriteLine("An  Instance  Created");
}
// Destructor
~SampleA()
{
Console.WriteLine("An  Instance  Destroyed");
}
}

class Program
{
public static void Test()
{
SampleA T = new SampleA(); // Created instance of class
}
static void Main(string[] args)
{
Test();
GC.Collect();
Console.ReadLine();
}
}
}
When we run above program it will show output like as shown below

Output


An instance created
An instance destroyed
I hope it helps you to know about destructor concept. If you want to know more about oops concept check this link oops concepts

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

4 comments :

Anonymous said...

Hello Suresh ,

I need a registration form with capcha image.How to check capcha text is valid or not,then proceed to login page..
Could u please help me.
Thanks , Saroj Panda

Kulwant said...

Great valuable blog loved it

Unknown said...
This comment has been removed by the author.
Anonymous said...

thnx

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.