C#

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 /*------------------------------------------------------------------------------------------------------------
 7  * 类_析构函数:
 8  *      1. 主要用于在类销毁之前释放类实例所使用的托管和非
 9  *           托管资源.
10  *      2. 隐式的调用了object基类的Finalize()方法
11  *      3. 无参
12  *      4. 无返回值
13  *      5. 不能在结构体中定义析构函数, 只能对类使用析构
14  *      6. 一个类只能有一个析构
15  *      7. 无法继承或者重载
16  *      8. 无法显示调用,只能由垃圾回收期自动调用
17 ------------------------------------------------------------------------------------------------------------*/
18 namespace 类_析构函数
19 {
20     class Person
21     {
22         public  Person()
23         {
24 
25         }
26         ~Person()
27         {
28             // 必须添加引用: System.Windows.Forms
29             System.Windows.Forms.MessageBox.Show("析构函数被调用了!");
30         }
31         /*----------------------------------------------------------------------------------------------
32          * ~Person() 析构函数被隐式的转换成下面的代码
33          * ----------------------------------------------------------------------------------------------      
34         protected override void Finalize()
35         {
36             try
37             {
38                 Console.WriteLine("析构函数被调用了!");
39             }
40             finally
41             {
42                 base.Finalize();
43             }
44         }
45         ----------------------------------------------------------------------------------------------*/
46 
47     }
48 
49     class Program
50     {
51         static void Main(string[] args)
52         {
53             Person p = new Person();
54             Console.ReadLine();
55         }
56     }
57 
58 }
原文地址:https://www.cnblogs.com/DuanLaoYe/p/5354474.html