简体   繁体   English

当dispose()方法可以写在类中时,为什么要使用IDisposable

[英]why use IDisposable when a dispose() method could be written in a class

Title explains the question quite clearly. 标题很清楚地解释了这个问题。 I want to know why would I implement IDisposable interface that provides only one method definition in a class, where, in another class, I can explicitly define a dispose() method and release all unmanaged resources. 我想知道为什么我要实现在类中只提供一个方法定义的IDisposable接口,在另一个类中,我可以显式定义dispose()方法并释放所有非托管资源。

Say I have these two classes 说我有这两个班

class MyClass : IDisposable
{
    public void Dispose() { }
}

class MyClass2
{
    public void Dispose() { }
}

What exactly is the difference between MyClass and MyClass2?? MyClass和MyClass2之间究竟有什么区别?

A very concrete consequence in this example, specific to IDisposable , is that the first class can be used in a using() {} statement and the second one cannot: 此示例中特定于IDisposable一个非常具体的结果是第一个类可以在using() {}语句中使用,第二个类不能:

using (var c1 = new MyClass())  { ... }  // Ok
using (var c2 = new MyClass2()) { ... }  // Error

The more general answer is that interfaces provide a contract that allows substitution. 更一般的答案是接口提供允许替换的合同。 Search for 'the benefits of interfaces' or something like that for more information. 搜索“接口的好处”或类似的东西以获取更多信息。 For instance this SO answer has a list. 例如, 这个SO答案有一个列表。

An example of substitution: 替换的一个例子:

public interface IRepository { ... }

public class RealRepository : IRepository { ... }
public class MockRepository : IRepository { ... }

class MyController { public IRepository { get; set; }

This (sketchy) fragment shows a Controller that can be used with a real (Db) Repository and tested with a Mock Repository. 这个(粗略的)片段显示了一个可以与真实(Db)存储库一起使用并使用模拟存储库进行测试的控制器。

Because IDisposable defines something of a universal contract of conventional semantics for freeing resources. 因为IDisposable定义了一些传统语义的通用契约,用于释放资源。 It's a known pattern that can be relied upon to expose a relevant method that does a certain thing (Henk gives a specific example of this in the language with using ). 它是可以依靠暴露,做某一件事有关方法的已知模式(亨克给出了一个具体的例子在语言与using )。 Nothing but the immediate user of your code knows of the existence of your class's Dispose method. 除了代码的直接用户,您知道类的Dispose方法是否存在。

In your first instance, the class implements the contract, and can be used in a conventional way. 在您的第一个实例中,该类实现了契约,并且可以以传统方式使用。

IDisposable is just one interface, but the question might be generalized "Why should I implement an interface when I can simply implement methods?". IDisposable只是一个接口,但问题可能是概括的“当我可以简单地实现方法时,为什么要实现接口?”。 Here is another example which might shed some light: it matters when it comes to consuming the classes. 这是另一个可能有些亮点的例子:当涉及到消费课程时,这很重要。

interface IAnimal
{
    void PutInZoo(Zoo);
}

class Cat: IAnimal
{
    public void PutInZoo(Zoo theZoo);
}

class Fish
{
   public void PutInZoo(Zoo theZoo);
}

class Zoo
{
    public void PutInZoo(IAnimal animal)
    {
         animal.PutInZoo(this);
    }

    public Zoo()
    {
         this.PutInZoo(new Cat());  // Ok
         this.PutInZoo(new Fish()); // Nope, Fish doesn't implement IAnimal
    }
}

The most prominent side effect of implementing an interface, aside from compilation errors, is that if the interface changes you must abide to it. 除编译错误外,实现接口最突出的副作用是, 如果接口发生变化,必须遵守它。

In this example, if IAnimal changes its definition and PutInZoo is renamed to FreeFromZoo , Fish would still compile (and probably break its consumers) but Dog won't: it no longer implements IAnimal . 在这个例子中,如果IAnimal改变它的定义并且PutInZoo被重命名为FreeFromZooFish仍然会编译(并且可能会破坏它的消费者)但是Dog不会:它不再实现IAnimal

The difference between the two is just that Class1 officially implements the interface. 两者之间的区别只是Class1 正式实现了接口。

Class2 just potentially implements it, although these terms aren't great; Class2可能会实现它,尽管这些术语并不好; the implementation/presence of the method may be the same for both. 两种方法的实现/存在可以是相同的。

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

相关问题 尝试在其他程序集中使用类时,IDisposable.Dispose不可用 - IDisposable.Dispose is not available when trying to use a class in other assembly 在实现IDisposable的类上正确使用Dispose方法 - Correct usage of Dispose method on class that implements IDisposable 为什么我的IDisposable对象上的Dispose()方法不可用? - Why is the Dispose() method not available on my IDisposable object? 如果没有Dispose方法,这个类如何实现IDisposable? - How does this class implement IDisposable if it doesn't have a Dispose method? 我们可以在不实现IDisposable接口的情况下使用Dispose方法吗? - Can we use Dispose method without Implementing IDisposable Interface? 正确实现Finalize和Dispose的方法(当父类实现IDisposable时) - Correct way of implementing Finalize and Dispose(When parent class implements IDisposable) 当我将它传递给IDisposable类时,我需要Dispose Stream吗? - Do i need to Dispose Stream when i Pass it to IDisposable class? 如何使用IDisposable处理对象 - How to use IDisposable to dispose an object 为什么我必须从IDisposable实现Dispose,为什么不只是一个简单的方法来释放...? - Why Do I have to implement Dispose from IDisposable, Why not just a simple method to release…? 检查方法是否使用反射实现IDisposable.Dispose - Check if method implements IDisposable.Dispose with reflection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM