为什么显式接口实现? [英] Why explicit interface implementation?

查看:207
本文介绍了为什么显式接口实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我实现了一个类,如:

I recently implemented a class like:

class TestClass : IDisposable
{
    RegistryKey m_key;
    public TestClass()
    {
        m_key = Registry.CurrentUser.OpenSubKey("Software", false);
    }

    public void Dispose()
    {
        // m_key.Dispose();
        IDisposable disp = m_key;
        disp.Dispose();
    }
}

如果我取消直接调用Dispose,我得到错误CS0117('Microsoft.Win32.RegistryKey'不包含'处置'的定义)。一些谷歌搜索使我这个线程,在那里我学到了什么事情,所以我现在明白它的机制。该 MSDN 文档建议笔者宁愿我调用close(),而不是Dispose()方法,但没有说明原因。

If I uncomment the direct call to Dispose, I get error CS0117 ("'Microsoft.Win32.RegistryKey' does not contain a definition for 'Dispose'"). Some Googling led me to this thread, where I learned what was going on, so I now understand the mechanics of it. The MSDN documentation suggests that the author would prefer that I call Close() instead of Dispose(), but doesn't explain why.

什么是这个模式的目的(我想我'已经在IO类见过它)?在这样的事实,这是由类作者故意决定光,是多么糟糕以上(电话通过IDisposable接口来处置)的代码?它也不能太糟糕 - 毕竟,这是会发生在using语句的,不是

What is the purpose of this pattern (which I think I've seen it in the IO classes as well)? In light of the fact that this was an intentional decision by the class author, how bad is the code above (the call to Dispose through the IDisposable interface)? It can't be too bad - after all, it's what would happen in a using statement, right?

推荐答案

这就是所谓的显式接口实现。在你的榜样,因为你定义了Dispose()方法为无效IDisposable.Dispose()你要明确实现IDisposable接口为好。

This is called explicit interface implementation. In your example since you define the Dispose() method as "void IDisposable.Dispose()" you are explicitly implementing the IDisposable interface as well.

这是通常做避免冲突。如果微软曾经想补充一点,做点别的事情的RegistryKey,他们将不能够除非他们使用显式实现该接口的另一个Dispose()方法。

This is normally done to avoid collisions. If Microsoft ever wanted to add another Dispose() method that did something else to RegistryKey they wouldn't be able to unless they used explicit implementation of that interface.

这是使用通用的IEnumerable 接口经常做。它需要你同时实现非通用IEnumerable接口。在这两个接口的唯一成员是GetEnumerator的,与普通的一个是更有用的,所以它通常是这样实现的:

This is done often with the generic IEnumerable<T> interface. It requires you to also implement the non-generic interface IEnumerable. The only member in these two interfaces is GetEnumerator, with the generic one being more useful, so its usually implemented like this:

public clas SomeClass : IEnumerable<SomeOtherClass>
{
    public IEnumerator<SomeOtherClass> GetEnumerator ()
    {
        ...
    }

    IEnumerator IEnumerable.GetEnumerator ()
    {
        return GetEnumerator ();
    }
}



这样,当你调用SomeClass的的GetEnumator方法的对象,它调用通用版本,因为其他人被明确地执行,使我们能够获得强类型泛型允许。

This way when you call an object of SomeClass's GetEnumator method, it calls the generic version, since the other one was implemented explicitly, allowing us to get the strong-typing generics allow.

请参阅网页166-169 编程C#按杰西自由(我已经得到了第四版)。

See pages 166-169 of Programming C# by Jesse Liberty (I've got the fourth edition).

这篇关于为什么显式接口实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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