Windows Phone Developers

Saturday, November 15, 2008

Destructors in C# (.NET)

Unlike constructors, destructors are sparsely used in C# programs. This is because .NET Framework does an excellent job of memory management using garbage collection. However, it there are too many unmanaged resources in the application, they can be used as follows

class ClsTea : IRetail

{

//Variables

private string _locale;

private static string _brand;

// Class Destructor

~ ClsTea()

{

Console.WriteLine("Destructor Called");

// Code to release objects

}

// Class Default Constructor

public ClsTea()

{

Console.WriteLine("Constructor Called");

_brand = "N/A";

}

  • A class can only have one destructor.
  • Destructors cannot be inherited or overloaded.
  • Destructors cannot be called. They are invoked automatically.
  • A destructor does not take modifiers or have parameters.

Destructors in .NET, .NET Destructors, How to create a destructor in .NET, How to use destructors in .NET,

See also

How to create and use Static Constructors in C# (.NET)

How to create Default Constructors in C#

What is an Abstract Class / Abstract Classes in C#

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

No comments:

Post a Comment