Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

c# - Definition of a method signature?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...