<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FClass%2Fdestructor</id>
		<title>Csharp/CSharp Tutorial/Class/destructor - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FClass%2Fdestructor"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Class/destructor&amp;action=history"/>
		<updated>2026-04-29T19:34:58Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Class/destructor&amp;diff=5701&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Class/destructor&amp;diff=5701&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:53Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Class/destructor&amp;diff=5702&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Class/destructor&amp;diff=5702&amp;oldid=prev"/>
				<updated>2010-05-26T12:16:15Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Base destructor==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
public class Base&lt;br /&gt;
{&lt;br /&gt;
   ~Base()&lt;br /&gt;
   {&lt;br /&gt;
      Console.WriteLine( &amp;quot;Base.~Base()&amp;quot; );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
public class Derived : Base&lt;br /&gt;
{&lt;br /&gt;
   ~Derived()&lt;br /&gt;
   {&lt;br /&gt;
      Console.WriteLine( &amp;quot;Derived.~Derived()&amp;quot; );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
   static void Main()&lt;br /&gt;
   {&lt;br /&gt;
      Derived derived = new Derived();&lt;br /&gt;
   }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Derived.~Derived()&lt;br /&gt;
Base.~Base()&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Demonstrate a destructor.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System; &lt;br /&gt;
 &lt;br /&gt;
class Destruct {  &lt;br /&gt;
  public int x;  &lt;br /&gt;
  &lt;br /&gt;
  public Destruct(int i) {  &lt;br /&gt;
    x = i;  &lt;br /&gt;
  }    &lt;br /&gt;
 &lt;br /&gt;
  // called when object is recycled &lt;br /&gt;
  ~Destruct() { &lt;br /&gt;
    Console.WriteLine(&amp;quot;Destructing &amp;quot; + x); &lt;br /&gt;
  } &lt;br /&gt;
}    &lt;br /&gt;
    &lt;br /&gt;
class DestructDemo {    &lt;br /&gt;
  public static void Main() {    &lt;br /&gt;
    Destruct ob = new Destruct(0); &lt;br /&gt;
 &lt;br /&gt;
    for(int i=1; i &amp;lt; 100; i++){ &lt;br /&gt;
       Destruct o = new Destruct(i); &lt;br /&gt;
    }&lt;br /&gt;
    Console.WriteLine(&amp;quot;Done&amp;quot;); &lt;br /&gt;
  }    &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Done&lt;br /&gt;
Destructing 99&lt;br /&gt;
Destructing 0&lt;br /&gt;
Destructing 98&lt;br /&gt;
Destructing 97&lt;br /&gt;
Destructing 96&lt;br /&gt;
Destructing 95&lt;br /&gt;
Destructing 94&lt;br /&gt;
Destructing 93&lt;br /&gt;
Destructing 92&lt;br /&gt;
Destructing 91&lt;br /&gt;
Destructing 90&lt;br /&gt;
Destructing 89&lt;br /&gt;
Destructing 88&lt;br /&gt;
Destructing 87&lt;br /&gt;
Destructing 86&lt;br /&gt;
Destructing 85&lt;br /&gt;
Destructing 84&lt;br /&gt;
Destructing 83&lt;br /&gt;
Destructing 82&lt;br /&gt;
Destructing 81&lt;br /&gt;
Destructing 80&lt;br /&gt;
Destructing 79&lt;br /&gt;
Destructing 78&lt;br /&gt;
Destructing 77&lt;br /&gt;
Destructing 76&lt;br /&gt;
Destructing 75&lt;br /&gt;
Destructing 74&lt;br /&gt;
Destructing 73&lt;br /&gt;
Destructing 72&lt;br /&gt;
Destructing 71&lt;br /&gt;
Destructing 70&lt;br /&gt;
Destructing 69&lt;br /&gt;
Destructing 68&lt;br /&gt;
Destructing 67&lt;br /&gt;
Destructing 66&lt;br /&gt;
Destructing 65&lt;br /&gt;
Destructing 64&lt;br /&gt;
Destructing 63&lt;br /&gt;
Destructing 62&lt;br /&gt;
Destructing 61&lt;br /&gt;
Destructing 60&lt;br /&gt;
Destructing 59&lt;br /&gt;
Destructing 58&lt;br /&gt;
Destructing 57&lt;br /&gt;
Destructing 56&lt;br /&gt;
Destructing 55&lt;br /&gt;
Destructing 54&lt;br /&gt;
Destructing 53&lt;br /&gt;
Destructing 52&lt;br /&gt;
Destructing 51&lt;br /&gt;
Destructing 50&lt;br /&gt;
Destructing 49&lt;br /&gt;
Destructing 48&lt;br /&gt;
Destructing 47&lt;br /&gt;
Destructing 46&lt;br /&gt;
Destructing 45&lt;br /&gt;
Destructing 44&lt;br /&gt;
Destructing 43&lt;br /&gt;
Destructing 42&lt;br /&gt;
Destructing 41&lt;br /&gt;
Destructing 40&lt;br /&gt;
Destructing 39&lt;br /&gt;
Destructing 38&lt;br /&gt;
Destructing 37&lt;br /&gt;
Destructing 36&lt;br /&gt;
Destructing 35&lt;br /&gt;
Destructing 34&lt;br /&gt;
Destructing 33&lt;br /&gt;
Destructing 32&lt;br /&gt;
Destructing 31&lt;br /&gt;
Destructing 30&lt;br /&gt;
Destructing 29&lt;br /&gt;
Destructing 28&lt;br /&gt;
Destructing 27&lt;br /&gt;
Destructing 26&lt;br /&gt;
Destructing 25&lt;br /&gt;
Destructing 24&lt;br /&gt;
Destructing 23&lt;br /&gt;
Destructing 22&lt;br /&gt;
Destructing 21&lt;br /&gt;
Destructing 20&lt;br /&gt;
Destructing 19&lt;br /&gt;
Destructing 18&lt;br /&gt;
Destructing 17&lt;br /&gt;
Destructing 16&lt;br /&gt;
Destructing 15&lt;br /&gt;
Destructing 14&lt;br /&gt;
Destructing 13&lt;br /&gt;
Destructing 12&lt;br /&gt;
Destructing 11&lt;br /&gt;
Destructing 10&lt;br /&gt;
Destructing 9&lt;br /&gt;
Destructing 8&lt;br /&gt;
Destructing 7&lt;br /&gt;
Destructing 6&lt;br /&gt;
Destructing 5&lt;br /&gt;
Destructing 4&lt;br /&gt;
Destructing 3&lt;br /&gt;
Destructing 2&lt;br /&gt;
Destructing 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Destructors==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;OL&amp;gt;&amp;lt;LI&amp;gt;A destructor method called just prior to an object&amp;quot;s final destruction by the garbage collector.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;A destructor can be used to ensure that an object terminates cleanly.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;Destructors have this general form:&amp;lt;/LI&amp;gt;&amp;lt;/OL&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;~class-name( ) {&lt;br /&gt;
// destruction code&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;class-name is the name of the class.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Illustrates a destructor==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;public class Car&lt;br /&gt;
{&lt;br /&gt;
  ~Car()&lt;br /&gt;
  {&lt;br /&gt;
    System.Console.WriteLine(&amp;quot;In ~Car() destructor&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  public static void Main()&lt;br /&gt;
  {&lt;br /&gt;
    Car myCar = new Car();&lt;br /&gt;
    System.Console.WriteLine(&amp;quot;At the end of Main()&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;At the end of Main()&lt;br /&gt;
In ~Car() destructor&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simple Finalize==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
class MyClass&lt;br /&gt;
{&lt;br /&gt;
  ~MyClass()&lt;br /&gt;
  {&lt;br /&gt;
    Console.WriteLine(&amp;quot;Finalizing&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    MyClass fc = new MyClass();&lt;br /&gt;
    Console.WriteLine(&amp;quot;Exiting main&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Exiting main&lt;br /&gt;
Finalizing&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Update static field in the deconstructor==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;public class MyClass&lt;br /&gt;
{&lt;br /&gt;
  private static int numberOfMyClass = 0;&lt;br /&gt;
  public MyClass()&lt;br /&gt;
  {&lt;br /&gt;
    System.Console.WriteLine(&amp;quot;Creating a MyClass object&amp;quot;);&lt;br /&gt;
    numberOfMyClass++;  &lt;br /&gt;
  }&lt;br /&gt;
  ~MyClass()&lt;br /&gt;
  {&lt;br /&gt;
    System.Console.WriteLine(&amp;quot;Destroying a MyClass object&amp;quot;);&lt;br /&gt;
    numberOfMyClass--;  // decrement numberOfMyClass&lt;br /&gt;
  }&lt;br /&gt;
  public static int GetNumberOfMyClass()&lt;br /&gt;
  {&lt;br /&gt;
    return numberOfMyClass;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  public static void Main()&lt;br /&gt;
  {&lt;br /&gt;
    System.Console.WriteLine(&amp;quot;MyClass.GetNumberOfMyClass() = &amp;quot; + MyClass.GetNumberOfMyClass());&lt;br /&gt;
    MyClass myMyClass = new MyClass();&lt;br /&gt;
    System.Console.WriteLine(&amp;quot;MyClass.GetNumberOfMyClass() = &amp;quot; + MyClass.GetNumberOfMyClass());&lt;br /&gt;
    MyClass myMyClass2 = new MyClass();&lt;br /&gt;
    System.Console.WriteLine(&amp;quot;MyClass.GetNumberOfMyClass() = &amp;quot; + MyClass.GetNumberOfMyClass());&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;MyClass.GetNumberOfMyClass() = 0&lt;br /&gt;
Creating a MyClass object&lt;br /&gt;
MyClass.GetNumberOfMyClass() = 1&lt;br /&gt;
Creating a MyClass object&lt;br /&gt;
MyClass.GetNumberOfMyClass() = 2&lt;br /&gt;
Destroying a MyClass object&lt;br /&gt;
Destroying a MyClass object&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use destructor and implement IDisposable==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
public class MyClass : IDisposable&lt;br /&gt;
{&lt;br /&gt;
  ~MyClass()&lt;br /&gt;
  {&lt;br /&gt;
    Console.WriteLine(&amp;quot;In destructor&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public void Dispose()&lt;br /&gt;
  {&lt;br /&gt;
    Console.WriteLine(&amp;quot;In Dispose()&amp;quot;);&lt;br /&gt;
    GC.SuppressFinalize(this);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
  public static void Main(string[] args)&lt;br /&gt;
  {    &lt;br /&gt;
    MyClass c1, c2, c3, c4;&lt;br /&gt;
    c1 = new MyClass();&lt;br /&gt;
    c2 = new MyClass();&lt;br /&gt;
    c3 = new MyClass();&lt;br /&gt;
    c4 = new MyClass();&lt;br /&gt;
    &lt;br /&gt;
    Console.WriteLine(&amp;quot;\n***** Disposing c1 and c3 *****&amp;quot;);&lt;br /&gt;
    c1.Dispose();&lt;br /&gt;
    c3.Dispose();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;***** Disposing c1 and c3 *****&lt;br /&gt;
In Dispose()&lt;br /&gt;
In Dispose()&lt;br /&gt;
In destructor&lt;br /&gt;
In destructor&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>