C#中函数名前的波浪号是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/188688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:17:39  来源:igfitidea点击:

What does the tilde before a function name mean in C#?

c#syntaxtilde

提问by Keith Sirmons

I am looking at some code and it has this statement:

我正在查看一些代码,它有以下声明:

~ConnectionManager()
{
    Dispose(false);
}

The class implements the IDisposableinterface, but I do not know if that is part of that the tilde(~) is used for.

该类实现了IDisposable接口,但我不知道这是否是波浪号(~)用于的一部分。

采纳答案by Patrick Desjardins

~ is the destructor

~ 是析构函数

  1. Destructors are invoked automatically, and cannot be invoked explicitly.
  2. Destructors cannot be overloaded. Thus, a class can have, at most, one destructor.
  3. Destructors are not inherited. Thus, a class has no destructors other than the one, which may be declared in it.
  4. Destructors cannot be used with structs. They are only used with classes. An instance becomes eligible for destruction when it is no longer possible for any code to use the instance.
  5. Execution of the destructor for the instance may occur at any time after the instance becomes eligible for destruction.
  6. When an instance is destructed, the destructors in its inheritance chain are called, in order, from most derived to least derived.
  1. 析构函数是自动调用的,不能显式调用。
  2. 析构函数不能重载。因此,一个类最多可以有一个析构函数。
  3. 析构函数不是继承的。因此,一个类除了可以在其中声明的析构函数之外没有其他析构函数。
  4. 析构函数不能与结构一起使用。它们仅用于类。当任何代码不再可能使用实例时,实例就有资格被销毁。
  5. 实例的析构函数可以在实例符合销毁条件后的任何时间执行。
  6. 当一个实例被析构时,它的继承链中的析构函数按从最派生到最少派生的顺序被调用。

Finalize

敲定

In C#, the Finalize method performs the operations that a standard C++ destructor would do. In C#, you don't name it Finalize -- you use the C++ destructor syntax of placing a tilde ( ~ ) symbol before the name of the class.

在 C# 中,Finalize 方法执行标准 C++ 析构函数会执行的操作。在 C# 中,您不将其命名为 Finalize —— 您使用 C++ 析构函数语法在类名称之前放置波浪号 ( ~ ) 符号。

Dispose

处置

It is preferable to dispose of objects in a Close()or Dispose()method that can be called explicitly by the user of the class. Finalize (destructor) are called by the GC.

最好在可以由类的用户显式调用的Close()orDispose()方法中处理对象。Finalize(析构函数)由 GC 调用。

The IDisposableinterface tells the world that your class holds onto resources that need to be disposed and provides users a way to release them. If you do need to implement a finalizer in your class, your Dispose method shoulduse the GC.SuppressFinalize()method to ensure that finalization of your instance is suppressed.

IDisposable的接口告诉世界,你的类保存到需要处置资源,并为用户提供一种方式来释放他们。如果确实需要在类中实现终结器,则 Dispose 方法使用该GC.SuppressFinalize()方法来确保抑制实例的终结。

What to use?

用什么?

It is not legal to call a destructor explicitly. Your destructor will be called by the garbage collector. If you do handle precious unmanaged resources (such as file handles) that you want to close and dispose of as quickly as possible, you ought to implement the IDisposable interface.

显式调用析构函数是不合法的。垃圾收集器将调用您的析构函数。如果您确实处理了想要尽快关闭和处理的宝贵的非托管资源(例如文件句柄),则应该实现 IDisposable 接口。

回答by torial

It is used to indicate the destructor for the class.

它用于指示类的析构函数。

回答by Santiago Palladino

Same as C++, it's the destructor; however in C# you don't called it explicitely, it is invoked when the object gets collected.

和C++一样,都是析构函数;但是在 C# 中,您不会明确调用它,而是在收集对象时调用它。

回答by stephenbayer

~ usually represents a deconstructor. which is run right before a object dies.

~ 通常代表一个解构器。它在对象死亡之前运行。

Here is a description of C# deconstructors i found

这是我发现的 C# 解构器的描述

回答by Jon Skeet

This is a finalizer. To be honest, you should very rarely need to write a finalizer. You really only need to write one if:

这是一个终结器。老实说,您应该很少需要编写终结器。你真的只需要在以下情况下写一个:

  • You have direct access to an unmanaged resource (e.g. through an IntPtr) and you can't use SafeHandlewhich makes it easier
  • You are implementing IDisposablein a class which isn't sealed. (My preference is to seal classes unless they're designed for inheritance.) A finalizer is part of the canonical Dispose pattern in such cases.
  • 您可以直接访问非托管资源(例如,通过IntPtr)并且您不能使用SafeHandle这使得它更容易
  • 您正在IDisposable一个未密封的类中实现。(我更喜欢密封类,除非它们是为继承而设计的。)在这种情况下,终结器是规范的 Dispose 模式的一部分。

回答by Jeff Stong

See Destructors (C# Programming Guide). Be aware, however that, unlike C++, programmer has no control over when the destructor is called because this is determined by the garbage collector.

请参阅析构函数(C# 编程指南)。但是请注意,与 C++ 不同,程序员无法控制何时调用析构函数,因为这是由垃圾收集器确定的。