Does implementing Interface both implicit and explicit make sense?

怎甘沉沦 提交于 2019-12-05 23:05:12

问题


I'm currently studying for my MS 70-515 exam. In one of the practices the author implements an interface both implicit as well as explicit. The explicit implementation just calls the implicit implementation. The explicit implementation is just listed without an explanation.

Does it make sense to have both an implicit and an explicit implementation of the interface? I would think the explicit implementation is redundant (in this case).

public class PassTextBox : TextBox, IScriptControl
{
    public virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
    {
        var descriptor = new ScriptControlDescriptor(
            "AjaxEnabled.PassTextBox", ClientID);
        // ...
        return new ScriptDescriptor[] {descriptor};
    }

    IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
    {
        return GetScriptDescriptors();
    }
}

BTW, the code seems to run just fine without the explicit implementation, as the implicit implementation is public.

It concerns MCTS Self-Paced Training Kit (Exam 70-515): Web Applications Development with Microsoft .NET Framework 4 Chapter 9, Lesson 2, Practice 3 to be precise.


回答1:


The explicit implementation seems to be totally superfluous.

I can't think of a way to call it where it would make a difference if you left it out.

There is one small difference, the implicit version is virtual meaning it could be overridden. The explicit version will always be called at this entry point. But since it only calls the virtual member that difference is not used here.



来源:https://stackoverflow.com/questions/10165296/does-implementing-interface-both-implicit-and-explicit-make-sense

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!