使用Interface的真正优势是什么 [英] what's real advantage of using Interface

查看:136
本文介绍了使用Interface的真正优势是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public interface IDisposable
{
   void Dispose();
}







-------------- ----------------------------




------------------------------------------

public class MyClass : IDisposable
{
    public void Dispose()
    {
        // Perform any object clean up here.

        // If you are inheriting from another class that
        // also implements IDisposable, don't forget to
        // call base.Dispose() as well.
    }
}





-------------------- --------------------



这里有什么'真正使用Interface Dispos能够。为什么不能直接执行Dispose menmber

我们必须实现Idisposable&然后在dispose方法中写入功能。这看起来很长。



首先我们必须创建类然后我们必须单独定义方法名称&然后课程终于实现了

&然后功能是用Dispose方法写的。那么这里有什么好处。



----------------------------------------

here in above what''s real use of Interface Disposable. why can''t the Dispose menmber directly implemented instead
we have to implemenmt Idisposable & then write functionality inside in dispose method.This looks like a long way.

First we have to create class then we have to define method name alone & then the class is finally implemneted
& then the fuctionality is to be written in Dispose method. so what''s advantage here.

推荐答案

如果你的类实现了 IDisposable 接口,那么它必须提供它自己实现的 Dispose 方法。请参阅正确实施IDisposable和Dispose模式 [ ^ ]。
If your class implements the IDisposable interface, then it must provide its own implementation of the Dispose method. See Implementing IDisposable and the Dispose Pattern Properly[^].


并非一切都是Disposeable - 或者需要。只有当它包含需要返回到需要实现Dispose的系统的资源时,所以当它实现时,这是一个好的迹象,你应该在完成它时调用Dispose ,或者您应该使用块在内创建它。



如果它始终可用,它只会让人们更难以知道是否应该处理类实例 - 而且大多数都不是这样他们没有Dispose方法来调用。
Not everything is Disposeable - or needs to be. It''s only when it contains resources which need to be returned to the system that you need to implement Dispose, so when it it implemented, that is a good sign that you should either be calling Dispose on it when you are finished with it, or you should be creating it within a using block.

If it was always available, it would just make it harder to know if the class instances should be disposed - and most don''t so they don''t have a Dispose method to call.


请通过这个链接。

http://stackoverflow.com/questions/538060/proper -use-of-idisposable-interface [ ^ ]



希望这有帮助
Please go through this link.
http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface[^]

Hope this helps


这篇关于使用Interface的真正优势是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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