与具有相同名称和不同返回类型的成员的接口

Sam*_*ach 2 .net c# interface method-signature

为什么我不能做以下事情?

public interface ICommunication
{
    int Send(Dictionary<string, string> d);
    int Send(byte[] b);

    Dictionary<string, string> Receive();
    byte[] Receive(); // Error
}
Run Code Online (Sandbox Code Playgroud)

符号Receive()是不同的,但参数是相同的.为什么编译器会查看参数而不是成员签名?

ICommunication'已经定义了一个名为'Receive'的成员,它具有相同的参数类型.

我怎么能绕过这个?

我可以重命名Receive()如下,但我更喜欢保持命名Receive().

public interface ICommunication
{
    int Send(Dictionary<string, string> d);
    int Send(byte[] b);

    Dictionary<string, string> ReceiveDictionary();
    byte[] ReceiveBytes(); 
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*oni 6

返回类型不是方法签名的一部分,因此从语言角度来看,接口将两次声明相同的方法.

来自Microsoft的C#编程指南:

出于方法重载的目的,方法的返回类型不是方法签名的一部分.但是,在确定委托与其指向的方法之间的兼容性时,它是方法签名的一部分.