什么是一个函数名前的波浪在C#是什么意思? [英] What does the tilde before a function name mean in C#?

查看:390
本文介绍了什么是一个函数名前的波浪在C#是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看一些code,它有这样的语句:

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

~ConnectionManager()
{
    Dispose(false);
}

此类实现的IDisposable 接口,但我不知道这是否是该符号(〜)用于组成部分。

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

推荐答案

〜就是析构函数


  1. 析构自动调用,并且不能明确地调用。

  2. 析构函数不能重载。因此,一个类可以有,最多只有一个析构函数。

  3. 析构函数是不能继承的。因此,一类有不超过一个,这可能在它被声明其他析

  4. 析构函数不能与结构使用。他们只与类使用。
    一个实例变得符合销毁条件,当它不再可能为任何code使用的实例。

  5. 的析构函数的实例后,随时可能发生的实例的执行变得符合销毁。

  6. 当一个实例遭到破坏,其继承链中的析构函数被调用,顺序,从最派生到最少的。

敲定

在C#中,Finalize方法执行的操作,一个标准的C ++的析构函数会做。在C#中,不要将其命名敲定 - 你用放置一个波浪号(〜)符号类的名称之前的C ++析构函数的语法。

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.

的Dispose

这是preferable在关闭()的Dispose()方法处理的对象的可以由类的用户明确调用。敲定(析构函数)是由GC调用。

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.

的IDisposable 的界面告诉你的类包含了上需要处理的资源并为用户提供一种方式来释放他们的世界。如果你需要实现一个终结在你的类,你的Dispose方法的的使用 GC.Sup pressFinalize()方法,以确保您的实例是最后定稿是pssed燮$ p $。

The IDisposable interface 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 should use the GC.SuppressFinalize() method to ensure that finalization of your instance is suppressed.

使用什么?

这是不合法的显式调用析构函数。您的析构函数会被垃圾收集器调用。如果你这样做处理precious非托管资源(如文件句柄)要关闭,并尽快处置,你应该实现IDisposable接口。

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.

这篇关于什么是一个函数名前的波浪在C#是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆