析构函数:
?一.析构函数:
?析构函数是c#用于专门释放被占用的系统资源的,在对象不再需要时,就会调用析构函数
?
?二.析构函数的性质:
?1.析构函数在类对象销毁时自动会执行
?2.一个类只能有一个析构函数,而且析构函数没有参数,即析构函数不能重载
?3.析构函数的名称是“~”加上类的名称(中间没有空格)
?4.与构造函数一样,析构函数没有返回类型
?5.析构函数不能被继承
?
?三、调用析构函数
?当一个对象被系统销毁时自动调用类的析构函数
?
?四、示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Proj6_3 { //调用析构函数---可以专门释放被占用的系统资源,没返回值,不能被继承 //当对象被销毁时,自动调用析构函数 class Program { public class TPoint2 { //声明类TPoint2 int x, y; public TPoint2(int x1, int y1) { x = x1; y = y1; } ~TPoint2() { Console.WriteLine("点=>({0},{1})", x, y); } } static void Main(string[] args) { TPoint2 p1 = new TPoint2(2, 6); TPoint2 p2 = new TPoint2(8, 3); } } }
?
?五、运行效果:
?
?六、解释
?上述中,通过带参数的构造函数给对象字段赋值,通过析构函数输出对象字段的值。
?
原文:http://cb123456.iteye.com/blog/2210008