微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c#界面问题

我有以下代码
// IMyInterface.cs

namespace InterfaceNamespace
{
    interface IMyInterface
    {
        void MethodToImplement();
    }
}

.

// InterfaceImplementer.cs
class InterfaceImplementer : IMyInterface
{
    void IMyInterface.MethodToImplement()
    {
        Console.WriteLine("MethodToImplement() called.");
    }
}

这段代码编译得很好(为什么?).但是,当我尝试使用它时:

// Main.cs

    static void Main()
    {
        InterfaceImplementer iImp = new InterfaceImplementer();
        iImp.MethodToImplement();
    }

我明白了:

06003

即从外部看不到MethodToImplement.但是,如果我做了以下更改:

// InterfaceImplementer.cs
class InterfaceImplementer : IMyInterface
{

    public void MethodToImplement()
    {
        Console.WriteLine("MethodToImplement() called.");
    }
}

然后Main.cs编译也很好.为什么这两者有区别?

解决方法

implementing an interface explicitly,您正在创建一个只能通过强制转换到接口来调用的私有方法.

原文地址:https://www.jb51.cc/csharp/99585.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐