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

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

问题描述

我最近实现了一个类:

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' 不包含 'Dispose' 的定义").一些谷歌搜索让我找到了这个主题,在那里我了解到发生了什么,所以我现在了解它的机制.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 接口调用 Dispose)有多糟糕?它不会太糟糕 - 毕竟,这是在 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() 方法定义为void 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.

这通常是为了避免冲突.如果 Microsoft 想添加另一个对 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.

请参阅 Jesse Liberty 撰写的 Programming C# 的第 166-169 页(我有第四版).

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

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

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