简体   繁体   English

如果我有一个Dispose方法,我必须实现IDisposable吗?

[英]If I have a Dispose method, must I implement IDisposable?

I have a class that contains a Dispose method. 我有一个包含Dispose方法的类。 In this method I abort a thread. 在这个方法中,我中止了一个线程。 Is it necessary to implement the IDisposable interface? 是否有必要实现IDisposable接口?

If you implement IDisposable interface then your class object could be used in using block that automatically call the Dispose method implicitly. 如果您实现了IDisposable接口,那么您的类对象可以用于使用隐式自动调用Dispose方法的块。

From the answer from JaredPar 来自JaredPar的答案

There are only 2 reasons for implementing IDisposable on a type 在类型上实现IDisposable只有两个原因

  • The type contains native resources which must be freed when the type is no longer used 该类型包含必须在不再使用该类型时释放的本机资源
  • The type contains fields of type IDisposable 该类型包含IDisposable类型的字段

Is there any benefit to implementing IDisposable on classes which do not have resources? 在没有资源的类上实现IDisposable有什么好处吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我必须从IDisposable实现Dispose,为什么不只是一个简单的方法来释放...? - Why Do I have to implement Dispose from IDisposable, Why not just a simple method to release…? 如果没有Dispose方法,这个类如何实现IDisposable? - How does this class implement IDisposable if it doesn't have a Dispose method? 当我的控制器的字段实现IDisposable时,该如何处置? - How do I dispose of my Controllers when their fields implement IDisposable? 我是否需要处置与应用程序本身具有相同生存期的IDisposable? - Do I need to dispose IDisposable that have the same lifetime as the application itself? 我是否必须在这种自定义对象中实现Dispose方法? - Do I have to implement a Dispose method in this kind of custom object? 我应该实现Dispose方法吗? - Should I implement a Dispose method? 我必须在所有类上实现IDisposable,还是基类足够? - Must I implement IDisposable on all classes, or is a base class sufficient? 如何在我的代码中处理IDisposable? - How can I dispose the IDisposable in my code? 对于继承自IDisposable的Microsoft构建类,我是否必须明确调用Dispose? - For Microsoft built classes that inherit IDisposable, do I explicitly have to call Dispose? 调用subject.OnCompleted()是否会整理/调用处置/不泄漏内存,还是我自己必须在IDisposable上调用处置? - Will calling subject.OnCompleted() tidy up/ call dispose/not leak memory or do I have to call dispose on the IDisposable myself?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM