Csharp/CSharp Tutorial/Class/destructor — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
Содержание
Base destructor
using System;
public class Base
{
~Base()
{
Console.WriteLine( "Base.~Base()" );
}
}
public class Derived : Base
{
~Derived()
{
Console.WriteLine( "Derived.~Derived()" );
}
}
public class MainClass
{
static void Main()
{
Derived derived = new Derived();
}
}
Derived.~Derived() Base.~Base()
Demonstrate a destructor.
using System;
class Destruct {
public int x;
public Destruct(int i) {
x = i;
}
// called when object is recycled
~Destruct() {
Console.WriteLine("Destructing " + x);
}
}
class DestructDemo {
public static void Main() {
Destruct ob = new Destruct(0);
for(int i=1; i < 100; i++){
Destruct o = new Destruct(i);
}
Console.WriteLine("Done");
}
}
Done Destructing 99 Destructing 0 Destructing 98 Destructing 97 Destructing 96 Destructing 95 Destructing 94 Destructing 93 Destructing 92 Destructing 91 Destructing 90 Destructing 89 Destructing 88 Destructing 87 Destructing 86 Destructing 85 Destructing 84 Destructing 83 Destructing 82 Destructing 81 Destructing 80 Destructing 79 Destructing 78 Destructing 77 Destructing 76 Destructing 75 Destructing 74 Destructing 73 Destructing 72 Destructing 71 Destructing 70 Destructing 69 Destructing 68 Destructing 67 Destructing 66 Destructing 65 Destructing 64 Destructing 63 Destructing 62 Destructing 61 Destructing 60 Destructing 59 Destructing 58 Destructing 57 Destructing 56 Destructing 55 Destructing 54 Destructing 53 Destructing 52 Destructing 51 Destructing 50 Destructing 49 Destructing 48 Destructing 47 Destructing 46 Destructing 45 Destructing 44 Destructing 43 Destructing 42 Destructing 41 Destructing 40 Destructing 39 Destructing 38 Destructing 37 Destructing 36 Destructing 35 Destructing 34 Destructing 33 Destructing 32 Destructing 31 Destructing 30 Destructing 29 Destructing 28 Destructing 27 Destructing 26 Destructing 25 Destructing 24 Destructing 23 Destructing 22 Destructing 21 Destructing 20 Destructing 19 Destructing 18 Destructing 17 Destructing 16 Destructing 15 Destructing 14 Destructing 13 Destructing 12 Destructing 11 Destructing 10 Destructing 9 Destructing 8 Destructing 7 Destructing 6 Destructing 5 Destructing 4 Destructing 3 Destructing 2 Destructing 1
Destructors
- A destructor method called just prior to an object"s final destruction by the garbage collector.
- A destructor can be used to ensure that an object terminates cleanly.
- Destructors have this general form:
~class-name( ) {
// destruction code
}
class-name is the name of the class.
Illustrates a destructor
public class Car
{
~Car()
{
System.Console.WriteLine("In ~Car() destructor");
}
}
class MainClass
{
public static void Main()
{
Car myCar = new Car();
System.Console.WriteLine("At the end of Main()");
}
}
At the end of Main() In ~Car() destructor
Simple Finalize
using System;
class MyClass
{
~MyClass()
{
Console.WriteLine("Finalizing");
}
}
class MainClass
{
static void Main(string[] args)
{
MyClass fc = new MyClass();
Console.WriteLine("Exiting main");
}
}
Exiting main Finalizing
Update static field in the deconstructor
public class MyClass
{
private static int numberOfMyClass = 0;
public MyClass()
{
System.Console.WriteLine("Creating a MyClass object");
numberOfMyClass++;
}
~MyClass()
{
System.Console.WriteLine("Destroying a MyClass object");
numberOfMyClass--; // decrement numberOfMyClass
}
public static int GetNumberOfMyClass()
{
return numberOfMyClass;
}
}
class MainClass
{
public static void Main()
{
System.Console.WriteLine("MyClass.GetNumberOfMyClass() = " + MyClass.GetNumberOfMyClass());
MyClass myMyClass = new MyClass();
System.Console.WriteLine("MyClass.GetNumberOfMyClass() = " + MyClass.GetNumberOfMyClass());
MyClass myMyClass2 = new MyClass();
System.Console.WriteLine("MyClass.GetNumberOfMyClass() = " + MyClass.GetNumberOfMyClass());
}
}
MyClass.GetNumberOfMyClass() = 0 Creating a MyClass object MyClass.GetNumberOfMyClass() = 1 Creating a MyClass object MyClass.GetNumberOfMyClass() = 2 Destroying a MyClass object Destroying a MyClass object
Use destructor and implement IDisposable
using System;
public class MyClass : IDisposable
{
~MyClass()
{
Console.WriteLine("In destructor");
}
public void Dispose()
{
Console.WriteLine("In Dispose()");
GC.SuppressFinalize(this);
}
}
public class MainClass
{
public static void Main(string[] args)
{
MyClass c1, c2, c3, c4;
c1 = new MyClass();
c2 = new MyClass();
c3 = new MyClass();
c4 = new MyClass();
Console.WriteLine("\n***** Disposing c1 and c3 *****");
c1.Dispose();
c3.Dispose();
}
}
***** Disposing c1 and c3 ***** In Dispose() In Dispose() In destructor In destructor