return 类型/return 值是方法签名的一部分?

The return type / return value is part of the signature of the method?

C# 文档Methods 状态

Methods are declared in a class, struct, or interface by specifying the access level such as public or private, optional modifiers such as abstract or sealed, the return value, the name of the method, and any method parameters. These parts together are the signature of the method.

public class Foo
{
    public int InstanceMethod1()
    {
        return 85;
    }

    public static string StaticMethod1()
    {
        return "Bar";
    }
}

关于上面的摘录,“return值”是指方法的“return类型”吗?因此,return 类型的方法(在上面的示例中 intstring)被认为是方法签名的一部分?

注意,我确实阅读了以下旁注“为了方法重载的目的,方法的 return 类型不是方法签名的一部分。但是,它是方法签名的一部分方法时确定委托和它指向的方法之间的兼容性。”...但是,我上面的例子与方法重载无关。

方法的 return 值必须严格遵循其方法的 return 类型,因此在谈论方法签名时,您可以在技术上互换使用这两个术语,看起来他们已经完成了在摘录中,尽管令人困惑。

在方法重载的情况下,return 类型不被视为方法签名的一部分,因为编译器无法单独根据 return 类型确定要使用哪些方法,因此 return 类型不包含在方法签名中。例如,考虑以下仅 return 类型不同的重载方法:

public int GetResult() { }
public double GetResult() { } 

如果我们要调用这个方法,编译器怎么知道要使用哪个方法?

var result = GetResult();

然而,正如语言定义所述:方法名称、泛型类型的数量、每个形式参数的数量和类型以及 out/ref/value 参数在重载时是方法签名的一部分,例如,如果您想要超载然后你可以这样做:

public int GetResult() { }
public int GetResult(int x) { }

在某些情况下,return 类型的方法被视为签名的一部分,例如委托,因为该方法必须具有与委托声明相同的 return 类型。根据 C# specification:

In the context of method overloading, the signature of a method does not include the return value. But in the context of delegates, the signature does include the return value. In other words, a method must have the same return type as the delegate.