它是很好的做法,由外部事件在IDiposable类的Dispose方法注销? [英] Is it good practice to unregister from external events in the Dispose method of an IDiposable class?

查看:128
本文介绍了它是很好的做法,由外部事件在IDiposable类的Dispose方法注销?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了出色答卷解释如何使用Dispose模式,也为什么它这样

I read the excellent answer explaining how to use the Dispose pattern, and also why it works that way.

正确使用IDisposable接口

该职位明确规定的,你将要使用的Dispose模式在两个不同的方案:

The post clearly states that you would want to use the Dispose pattern in two different scenarios:


  1. 摆脱非托管资源的(因为我们要)

  2. 摆脱管理的资源(因为我们希望对大家有所帮助)

我的问题是:


  • 当一个对象被订阅了它的整个生命周期的外部事件,是它也是常见/好的做法从Dispose方法该事件注销?你会实现IDisposable接口用于这一目的?

推荐答案

是的,你应该。

这是指示向消费者类,它具有必须被释放的资源的最佳方式。 (即使事件订阅在技术上不是资源)

This is the best way to indicate to consumers of your class that it has "resources" that must be released. (even though event subscriptions are technically not resources)

这篇关于它是很好的做法,由外部事件在IDiposable类的Dispose方法注销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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