方法签名的定义?
方法签名(或方法签名)的正确定义是什么?
在谷歌上,我找到了各种定义:
是方法名和参数列表的组合
,这意味着方法签名=方法名+参数列表
吗?那么我没有看到“方法”和“方法签名”之间的区别。
如果我有一个方法:
public void Foo(int x, int y) { ... }
我的方法签名会是以下之一,还是两者都不是?
- Foo
- Foo(int, int)
- Foo(int x, int y)
- Foo(34, 78)
如果有人问我该方法的方法签名是什么,我该如何回答?
What is the correct definition of a method signature (or a signature of a method)?
On google, I find various definitions:
It is the combination of the method name and the parameter list
Does that mean method signature = method name + argument list
? Then I do not see difference between "method" and "method signature".
If I have a method:
public void Foo(int x, int y) { ... }
Would my method signature be one of the following, or neither?
- Foo
- Foo(int, int)
- Foo(int x, int y)
- Foo(34, 78)
How am I suppose to answer if someone ask me what is the method signature of the method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这里有很多正确的答案,它们将方法签名定义为方法名称、泛型数量、形式参数数量以及形式参数类型和种类,但不是返回类型或“params”修饰符。
虽然这是正确的,但这里有一些微妙之处。 C# 语言定义方法签名的方式与 CLR 定义方法签名的方式不同,这在 C# 和其他语言之间进行互操作时可能会导致一些有趣的问题。
对于 CLR,方法签名由方法名称、泛型数量、形式参数数量、形式参数类型和种类以及返回类型组成。所以有第一个区别; CLR 考虑返回类型。
CLR 也不认为“out”和“ref”是不同的形式参数类型; C# 确实如此。
CLR 还有一个有趣的功能,称为“可选和必需的类型修饰符”,通常称为“modopts”和“modreqs”。可以使用告诉您“主”类型的另一种类型来注释方法签名中的类型。例如,在 C++ 中,这是两个不同的签名:
这两个签名都定义了一个方法 M,该方法采用“对 C 的引用”类型的参数。但因为第一个是 const 引用而第二个不是,所以 C++ 语言将它们视为不同的签名。 CLR 通过允许 C++/CIL 编译器为形式参数类型上的特殊“this is const”类型发出 modopt 来实现此目的。
在 C# 中无法读取或设置 mod,但 C# 编译器仍然了解它们,并且会以某种方式尊重它们。例如,如果您在 C++/CIL 中声明了一个公共虚拟方法,如下所示:
并且您在用 C# 编写的派生类中重写该方法,则 C# 编译器将不会为您强制执行 const 正确性; C# 编译器不知道 const modopt 的含义。但 C# 编译器将确保重写方法在 modopt 就位的情况下发送到元数据中。这是必要的,因为 CLR 需要重写方法和被重写方法的签名相匹配;编译器必须遵守签名匹配的 CLR 规则,而不是 C# 规则。
There are a number of correct answer here which define the method signature as the method name, generic arity, formal parameter arity and formal parameter types and kinds, but not the return type or "params" modifier.
Though that is correct, there are some subtleties here. The way the C# language defines method signature is different from the way the CLR defines method signature, and that can lead to some interesting problems when interoperating between C# and other languages.
For the CLR, a method signature consists of the method name, generic arity, formal parameter arity, formal parameter types and kinds, and return type. So there is the first difference; the CLR considers return type.
The CLR also does not consider "out" and "ref" to be of different formal parameter kinds; C# does.
The CLR also has an interesting feature called "optional and required type modifiers", usually called "modopts" and "modreqs". It is possible to annotate a type in a method signature with another type that tells you about the "main" type. For example, in C++ these are two different signatures:
Both signatures define a method M that takes a parameter of type "reference to C". But because the first one is a const reference and the second is not, the C++ language considers these to be different signatures. The CLR implements this by allowing the C++/CIL compiler to emit a modopt for a special "this is const" type on the formal parameter type.
There is no way to read or set a mod in C#, but the C# compiler nevertheless knows about them and will honour them in some ways. For example, if you had a public virtual method declared in C++/CIL like:
and you override that in a derived class written in C#, the C# compiler will not enforce const correctness for you; the C# compiler has no idea what the const modopt means. But the C# compiler will ensure that the overriding method is emitted into metadata with the modopt in place. This is necessary because the CLR requires signatures of overriding and overridden methods to match; the compiler has to obey the CLR rules for signature matching, not the C# rules.
来自 MSDN:
这里重要的部分是方法的返回类型不属于其签名。因此,您不能重载仅因返回类型而异的方法!
From MSDN:
The important part here is that the return type of a method does not belong to its signature. So you can't overload methods which differ by return type only!
方法签名是编译器可以用来识别方法的方法属性集。
属性有:方法名称、参数数量、参数类型和参数顺序。
不同方法签名的示例:
所有这些方法都是不同的,当您在代码中调用它们时,编译器可以使用方法签名推断出您打算执行哪个方法。
另外,不要忘记模块化编程中方法的范围。
Method signature is the set of attributes of a method that a compiler can use to identify the method.
The attributes are: Method name, number of parameters, parameter type and order of parameters.
Example of different method signatures:
All of these methods are different, and when you call them in code the compiler can infer which method are you intent to execute using the method signature.
Also, don't forget the scope of the methods in modular programming.
方法签名包括以下几项:
参数的顺序。
注意:返回类型不是签名的一部分。
The method signature includes following items:
order of the parameters.
Note: The return type is not part of the signature.
在上面的示例中,方法签名是 Foo(int x, int y)。了解这一点很重要,因为在允许方法重载的语言(例如 C# 和 Java)中,方法名称将相同,但签名必须不同。
In your example above the method signature is Foo(int x, int y). This is important to know because in languages which allow method overloading (such as C# and Java), the method name will be the same, but the signature must be different.
C# 语言规范 (v 4.0) 的第 3.6 节提供了有关方法签名的最精确答案:
Section 3.6 of the C# Language Specification (v 4.0) provides the most precise answer regarding method signatures:
MSDN 文章 “方法(C# 编程指南)”
讲述:
此外,我在其他答案中还没有看到:
MSDN article "Methods (C# Programming Guide)"
tells:
And further on, what I have not seen amongst other answers: