Correct use of IDisposable pattern when using Managed C++ wrapper in C#

My C# class creates and uses Managed C++ object that wraps (allocates and uses) unmanaged C++ objects and resources. The Managed C++ class correctly implements IDisposable with Destructor and Finalizer. Therefore, it appears that my C# class should also implement IDisposable. I want to follow correct IDisposable pattern in C# as well. The following is unclear to me: Within Dispose method o

在C#中使用托管C ++包装器时正确使用IDisposable模式

我的C#类创建并使用包装(分配和使用)非托管C ++对象和资源的托管C ++对象。 Managed C ++类使用Destructor和Finalizer正确实现了IDisposable。 因此,看来我的C#类也应该实现IDisposable。 我想要在C#中遵循正确的IDisposable模式。 以下对我不清楚: 在我的C#类的Dispose方法中,我应该将托管C ++对象视为托管还是非托管(因为它们在内部依赖于非托管资源)? 是的,你的C#类也应该实现IDisposable。 它的Disp

Finalizer and IDisposable

Based on the documentation (MSDN: link), it is clear that one should use the IDisposable pattern when implementing a finalizer. But do you need to implement a finalizer if you implement IDisposable (so as to provide a deterministic way of disposing the object), and you dont have any unmanaged resources to clean up? As I see it, if the class has only managed resources and if you dont call Disp

Finalizer和IDisposable

基于文档(MSDN:链接),很明显,在实现终结器时应该使用IDisposable模式。 但是如果你实现了IDisposable(为了提供一种确定的方式来处理对象),你是否需要实现一个终结器,并且你没有任何非托管资源来清理? 正如我所看到的,如果该类只有托管资源,并且不调用Dispose,则受管资源将自动由GC清理,因此不需要实现终结器。 我错了吗? 此外,如果我正在使用Dispose方法清理事件处理程序,该怎么办? 由于Dispose不会自

How to Dispose an object using Dispose method

This question already has an answer here: Proper use of the IDisposable interface 18 answers The dispose method works together with the using statement. It will automatically called if a using block is closed. class A : IDisposable { public void Dispose() { // Dispose } } using (A a = new A()) { } You also don't need to override the method, because it is defined i

如何使用Dispose方法处理对象

这个问题在这里已经有了答案: 正确使用IDisposable接口18的答案 dispose方法与using语句一起using 。 如果using块被关闭,它会自动调用。 class A : IDisposable { public void Dispose() { // Dispose } } using (A a = new A()) { } 您也不需要override该方法,因为它是在界面中定义的。 在任何情况下using都将被调用。

Implement IDisposable

This question already has an answer here: Proper use of the IDisposable interface 18 answers Given that it uses explicit interface implementation, it's not clear to me that they want you to, but you can: public class MyClass : IDisposable { private WebServiceHost m_WebServiceHost; // Members public void Dispose() { ((IDisposable)m_WebServiceHost).Dispose(); } }

实现IDisposable

这个问题在这里已经有了答案: 正确使用IDisposable接口18的答案 鉴于它使用明确的接口实现,我不清楚他们想要你,但你可以: public class MyClass : IDisposable { private WebServiceHost m_WebServiceHost; // Members public void Dispose() { ((IDisposable)m_WebServiceHost).Dispose(); } } 我猜想他们更喜欢你只需要调用Close() ,但是我不能从文档中支持它。 像这样做: public clas

What is the simplest IDisposable pattern in c#?

This question already has an answer here: Proper use of the IDisposable interface 18 answers You almost had it, because your class is not sealed someone could derive from your class and that derived class could also need to dispose a object. Make your class sealed and your current implementation is fine. public sealed class ManagedResourceClient : IDisposable { private ITheManagedResour

什么是C#中最简单的IDisposable模式?

这个问题在这里已经有了答案: 正确使用IDisposable接口18的答案 你几乎已经拥有了它,因为你的类没有被密封的人可以从你的类派生出来,派生类也可能需要处理一个对象。 让你的课程密封起来,你现在的实施很好。 public sealed class ManagedResourceClient : IDisposable { private ITheManagedResource _myManagedResource = new TheManagedResource() public void Dispose() { if ( _myManagedReso

What is the correct way to implement IDisposable in C#?

This question already has an answer here: Proper use of the IDisposable interface 18 answers You can find good explanation here Proper use of the IDisposable interface The short example: public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { // clean resources here

什么是在C#中实现IDisposable的正确方法?

这个问题在这里已经有了答案: 正确使用IDisposable接口18的答案 你可以在这里找到很好的解释。正确使用IDisposable接口 简短的例子: public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { // clean resources here } disposed = true; } } private bool dispos

Implement IDisposable in abstract class

This question already has an answer here: Proper use of the IDisposable interface 18 answers I would take the implementation provided in this answer: Proper use of the IDisposable interface And in derived types override the Dispose(bool disposing) method if the derived type needs to also dispose of stuff, remembering to call base. If it doesn't need to, then simply do nothing with th

在抽象类中实现IDisposable

这个问题在这里已经有了答案: 正确使用IDisposable接口18的答案 我会采取在这个答案中提供的实现: 正确使用IDisposable接口 如果派生类型也需要处理东西,记住调用base,那么在派生类型中重写Dispose(bool disposing)方法。 如果不需要,那么根本不需要执行任何操作。 确保公开需要在此类型的公共合同上调用Dispose是有意义的,这可能是泄漏抽象(尽管不总是)的标志。 这至少是一个好的开始。

How does .Dispose() work in C#?

Possible Duplicate: Proper use of the IDisposable interface When is Dispose necessary? If I write a class, which uses graphics, or threads for example, must I implement the IDisposable interface to write a Dispose() method or else memory leak occurs? Does Dipose() run when the GC reaches it? If I implement a Dispose() method, shall I call Dispose() on all the disposable fields in the clas

.Dispose()如何在C#中工作?

可能重复: 正确使用IDisposable接口 Dispose何时需要? 如果我编写一个使用图形或线程的类,我是否必须实现IDisposable接口来编写Dispose()方法,否则会发生内存泄漏? 当GC达到它时,Dipose()会运行吗? 如果我实现了一个Dispose()方法,当“disposing”参数为true或者它是自动的时,我应该在类中的所有一次性字段上调用Dispose()吗? 什么是“非托管资源”? 所以我很不确定,我会很感激任何能帮助我理解这些

Is this how to "Implement IDisposable correctly"?

This question already has an answer here: Proper use of the IDisposable interface 18 answers IDisposable GC.SuppressFinalize(this) location 5 answers

这是如何“正确实施IDisposable”?

这个问题在这里已经有了答案: 正确使用IDisposable接口18的答案 IDisposable GC.SuppressFinalize(this)location 5 answers

How to destroy an object in C#

This question already has an answer here: Proper use of the IDisposable interface 18 answers 除了将对象设置为null之外,还应该将其从其他对象中移除,如果您使用的资源需要释放,请使用IDisposable public class Car: IDisposable { // free resources public void Dispose() { } } set c = null; As long as there are no other references to it, the garbage collector will destroy it the next

如何在C#中销毁对象

这个问题在这里已经有了答案: 正确使用IDisposable接口18的答案 除了将对象设置为null之外,还应该将其从其他对象中移除,如果您使用的资源需要释放,请使用IDisposable public class Car: IDisposable { // free resources public void Dispose() { } } 设置c = null; 只要没有其他引用,垃圾收集器将在下次运行时将其销毁。