17.12 Destructors

原创 2006年05月21日 02:53:00
A destructor is a member that implements the actions required to destruct an
instance of a class. A destructor is
declared using a destructor-declaration:
attributesopt externopt ~ identifier ( ) destructor-body
A destructor-declaration may include a set of attributes (§24).
The identifier of a destructor-declarator must name the class in which the
destructor is declared. If any other
name is specified, a compile-time error occurs.
When a destructor declaration includes an extern modifier, the destructor
is said to be an external destructor.
Because an external destructor declaration provides no actual
implementation, its destructor-body consists of a
semicolon. For all other destructors, the destructor-body consists of a
block, which specifies the statements to
execute in order to destruct an instance of the class. A destructor-body
corresponds exactly to the method-body of
an instance method with a void return type (§17.5.8).
Destructors are not inherited. Thus, a class has no destructors other than
the one which may be declared in that
[Note: Since a destructor is required to have no parameters, it cannot be
overloaded, so a class can have, at most,
one destructor. end note]
Destructors are invoked automatically, and cannot be invoked explicitly. An
instance becomes eligible for
destruction when it is no longer possible for any code to use that
instance. Execution of the destructor for the
instance may occur at any time after the instance becomes eligible for
destruction. When an instance is
destructed, the destructors in that instance?s inheritance chain are
called, in order, from most derived to least
derived [Example: The output of the example
Chapter 17 Classes
using System;
class A
~A() {
Console.WriteLine("A’s destructor");
class B: A
~B() {
Console.WriteLine("B’s destructor");
class Test
static void Main() {
B b = new B();
b = null;
B?s destructor
A?s destructor
since destructors in an inheritance chain are called in order, from most
derived to least derived. The GC.Collect
method is not required, but may be provided by an implementation. end
Destructors may be implemented by overriding the virtual method Finalize on
System.Object. In any event,
C# programs are not permitted to override this method or call it (or
overrides of it) directly. [Example: For
instance, the program
class A
override protected void Finalize() {} // error
public void F() {
this.Finalize(); // error
contains two errors. end example]
The compiler behaves as if this method, and overrides of it, does not exist
at all. [Example: Thus, this program:
class A
void Finalize() {} // permitted
is valid and the method shown hides System.Object?s Finalize method. end
For a discussion of the behavior when an exception is thrown from a
destructor, see §23.3.

Aspose.Words 12月新版17.12发布| 实施.NET标准2.0

Aspose.Words 12月新版17.12发布,实施.NET标准2.0.
  • cocacola456
  • cocacola456
  • 2017年12月07日 13:41
  • 115

Centos 7.4 Install docker 17.12

依赖安装 yum install -y checkpolicy audit-libs  audit audit-libs-python python-IPy  setools-libs libsem...
  • hanzheng260561728
  • hanzheng260561728
  • 2018年01月11日 14:55
  • 572

PDF控件Aspose.Pdf 12月新版17.12发布 | 附下载

Aspose.Pdf for .NET 17.12 更新  功能和改进 核心 概述 类别 PDFNET-43759 实现LaTeX片段嵌入 新功能 PD...
  • cocacola456
  • cocacola456
  • 2017年12月13日 14:22
  • 137

浅谈C++的class (初学者请看) 高手老手不用浪费网费

作为面向对象程序设计的基础,掌握class的基本结构和特性是十分重要的。 1.       如何实现一个classclass egclass {public:       //public提供了一组...
  • langhaixin
  • langhaixin
  • 2002年02月11日 09:18
  • 1185

Email 控件Aspose.Email 12月新版17.12发布 | 附下载

Email 控件Aspose.Email 12月新版17.12发布,修复了多个重大bug.
  • cocacola456
  • cocacola456
  • 2017年12月21日 14:18
  • 52

类的构造函数和析构函数 Class Constructors and Destructors

首先要说明,这两个字中国人是乱翻译的,像这样的翻译还有很多,估计哪个砖家喝高了,duang 一下,就翻译成这样了 socket 套接字 handle 句柄 instance 实例 字典上const...
  • robinsongsog
  • robinsongsog
  • 2015年06月07日 00:11
  • 893

​使用 Moby 构建 Docker for Mac 17.06 社区版

出品丨Docker公司(ID:docker-cn)编译丨小东每周一、三、五晚6点10分  与您不见不散说在前面Docker For Mac 17.06 社区版是第一个完全建立在 Moby 项目上的 D...
  • dt763C
  • dt763C
  • 2018年02月23日 00:00
  • 33

8.7.9 Destructors

A destructor is a member that implements the actions required to destruct an instance of a class. De...
  • masterall
  • masterall
  • 2005年11月30日 19:12
  • 556

Destructors in C#

Destructors in C#By AnsilIntroductionIn the enterprise application development world, the buzzwords ...
  • soRen
  • soRen
  • 2005年04月14日 12:24
  • 814
您举报文章:17.12 Destructors