前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CA1821:移除空终结器

CA1821:移除空终结器

作者头像
呆呆
发布2022-02-19 19:01:47
2600
发布2022-02-19 19:01:47
举报
文章被收录于专栏:centosDaicentosDai

规则 ID

CA1821

类别

“性能”

修复是中断修复还是非中断修复

非中断

原因

类型实现了一个空的终结器,只调用基类型终结器或只调用条件性发出的方法。

规则说明

应尽可能避免终结器,因为跟踪对象生存期会产生额外的性能系统开销。 垃圾回收器在收集对象之前运行终结器。 这意味着收集对象至少需要两个集合。 空的终结器只会徒增开销,没有一点好处。

如何解决冲突

移除空的终结器。 如果调试需要终结器,请将整个终结器置于 #if DEBUG / #endif 指令中。

何时禁止显示警告

不禁止显示此规则发出的消息。

示例

下面的示例演示了应移除的空终结器、应置于 #if DEBUG / #endif 指令中的终结器以及正确使用 #if DEBUG / #endif 指令的终结器。

public class Class1

{

// Violation occurs because the finalizer is empty.

~Class1()

{

}

}

public class Class2

{

// Violation occurs because Debug.Fail is a conditional method.

// The finalizer will contain code only if the DEBUG directive

// symbol is present at compile time. When the DEBUG

// directive is not present, the finalizer will still exist, but

// it will be empty.

~Class2()

{

Debug.Fail("Finalizer called!");

}

}

public class Class3

{

#if DEBUG

// Violation will not occur because the finalizer will exist and

// contain code when the DEBUG directive is present. When the

// DEBUG directive is not present, the finalizer will not exist,

// and therefore not be empty.

~Class3()

{

Debug.Fail("Finalizer called!");

}

#endif

}

本文系外文翻译,前往查看

如有侵权,请联系?cloudcommunity@tencent.com?删除。

本文系外文翻译前往查看

如有侵权,请联系?cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com