gpt4 book ai didi

c# - 与具有相同名称和不同返回类型的成员接口(interface)

转载 作者:行者123 更新时间:2023-11-30 19:17:08 25 4
gpt4 key购买 nike

为什么我不能执行以下操作?

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

Dictionary<string, string> Receive();
byte[] Receive(); // Error
}

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

ICommunication' already defines a member called 'Receive' with the same parameter types.

我该如何解决这个问题?

我可以重命名 Receive() 如下,但我更愿意将其命名为 Receive()

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

Dictionary<string, string> ReceiveDictionary();
byte[] ReceiveBytes();
}

最佳答案

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

来自 Microsoft's C# Programming Guide :

A return type of a method is not part of the signature of the method for the purposes of method overloading. However, it is part of the signature of the method when determining the compatibility between a delegate and the method that it points to.

关于c# - 与具有相同名称和不同返回类型的成员接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19316042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com