方法签名的定义? [英] Definition of a method signature?

查看:128
本文介绍了方法签名的定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法签名(或方法签名)的正确定义是什么?

在Google上,我找到了各种定义:

它是方法名称和参数列表的组合

这是否意味着method signature = method name + argument list?然后我看不到"方法"和"方法签名"之间的区别.

如果我有方法:

public void Foo(int x, int y) { ... }

我的方法签名是以下之一,还是都不是?

  • Foo
  • Foo(int,int)
  • Foo(int x,int y)
  • Foo(34,78)

如果有人问我方法的方法签名是什么,我该怎么回答?

解决方案

此处有许多正确答案,这些定义将方法签名定义为方法名称,通用Arity,形式参数Arity和形式参数类型与种类,但是不是返回类型或"params"修饰符.

尽管这是正确的,但这里还是有一些细微之处. C#语言定义方法签名的方式与 CLR 定义方法签名的方式不同,当在C#和其他语言之间进行互操作时,这可能会导致一些有趣的问题./p>

对于CLR,方法签名由方法名称,通用Arity,形式参数Arity,形式参数类型和种类以及返回类型组成.所以有第一个区别; CLR考虑返回类型.

CLR也不认为"out"和"ref"具有不同的形式参数类型; C#可以.

CLR还具有一个有趣的功能,称为可选和必需的类型修饰符",通常称为"modopts"和"modreqs".可以在方法签名中用另一种类型注释类型,以告诉您有关主要"类型的信息.例如,在C ++中,这是两个不同的签名:

void M(C const & x);
void M(C & x);

两个签名都定义了一个方法M,该方法采用类型为对C的引用"的参数.但是因为第一个是const引用,第二个不是const引用,所以C ++语言将它们视为不同的签名. CLR通过允许C ++/CIL编译器针对形式参数类型上的特殊"this is const"类型发出modopt来实现此目的.

无法在C#中读取或设置mod,但是C#编译器仍然了解它们并会以某种方式尊重它们.例如,如果您有一个在C ++/CIL中声明的公共虚拟方法,例如:

void V(C const * x)

,并且您重写了用C#编写的派生类中的内容,C#编译器将为您强制执行const正确性; C#编译器不了解const modopt的含义.但是C#编译器将确保在使用modopt的情况下将覆盖方法发送到元数据中.这是必要的,因为CLR需要覆盖的签名和被覆盖的方法来匹配.编译器必须遵循CLR规则进行签名匹配,而不是C#规则.

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?

解决方案

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:

void M(C const & x);
void M(C & x);

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:

void V(C const * x)

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.

这篇关于方法签名的定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆