2

MSDN here https://msdn.microsoft.com/en-us/library/ms173114.aspx says access modifiers like "private/protected" are part of method signature in c#.

However this link below doesnt seem to think so Method Signature in C#

Which one is it? Also what about a static method? Is the keyword "static" part of method signature?

thanks

Community
  • 1
  • 1
nesh_s
  • 399
  • 4
  • 19
  • You are mixing what in that page is called *part of the signature of the method for the purposes of method overloading* and the "general" method signature... in that page it is said that the return type isn't part of the "signature for method overloading"... clearly even the `abstract`/`sealed`/`public`/`private` parts aren't part of it. – xanatos Jul 03 '15 at 10:31
  • thanks. i was keen to understand the "general" method signature guidelines. does "static" form part of method signature? – nesh_s Jul 03 '15 at 10:36
  • I think a good answer to this would clarify what is meant by "method signature", how it is used, and therefore why there might be more than one definition. – IMSoP Jul 03 '15 at 10:42
  • In general, the response of Lipper [here](http://stackoverflow.com/a/8809191/613130) is good enough for your question (that isn't really a duplicate of the other question... simply put, the answer is good for both) – xanatos Jul 03 '15 at 10:43

3 Answers3

4

C# 5.0 specification, 1.6.6.Methods:

The signature of a method consists of the name of the method, the number of type parameters and the number, modifiers, and types of its parameters. The signature of a method does not include the return type.

CLI specification, I.8.6.1.5 Method signatures:

  • a calling convention*
  • the number of generic parameters, if the method is generic,
  • if the calling convention specifies this is an instance method and the owning method definition belongs to a type T then the type of the this pointer is ... [irrelevant here]
  • a list of zero or more parameter signatures—one for each parameter of the method and,
  • a type signature for the result value, if one is produced.

Notes:

* Calling convention includes static/instance specification.

For reference, II.15.3 Calling convention:

A calling convention specifies how a method expects its arguments to be passed from the caller to the called method. It consists of two parts: the first deals with the existence and type of the this pointer, while the second relates to the mechanism for transporting the arguments.

Conclusion: none of the definitions of method signature includes access modifiers.

Andrey
  • 59,039
  • 12
  • 119
  • 163
  • 1
    But as written here http://stackoverflow.com/a/8809191/613130, *There are two definitions of method signature. The C# language definition* and *The CLR, however, ...* – xanatos Jul 03 '15 at 10:34
  • @xanatos ok this is interesting spin, I don't think CLR has public specification, so how do we know how CLR defines signature. – Andrey Jul 03 '15 at 10:35
  • Clearly there is a public specification... there is a whole ECMA standard that describes how an assembly is constructed... ECMA-335 – xanatos Jul 03 '15 at 10:36
  • @xanatos CLR and CLI is not the same though, but yeah this topic is covered extensively in ECMA-335 – Andrey Jul 03 '15 at 10:46
  • so the access modifiers and "static" ARE part of the signature then? according to C#? – nesh_s Jul 03 '15 at 10:47
  • 2
    @nesh_s in CLI definition: static - yes, access modifiers - no. In C# definition: no for both. – Andrey Jul 03 '15 at 10:48
0

In don't believe is static is part of the method signature because even though static methods are called:

Classname.StaticMethodName(..);

While instance methods are called:

var o = new Classname();
o.MethodName(..);

It still defines methods and parameters that match for the signature. See this for more on the static vs instance methods and signature: Static and Instance methods with the same name?

Access level is not part of the signature because you can't have:

public void DoThis();

private void DoThis();

Both methods have the same signature because signature is based on method, generic parameter(s), and method parameter(s)/types.

The following are valid:

public void DoThis();
private void DoThis(int x);

OR:

public void DoThis();
private int DoThis<int>();
Community
  • 1
  • 1
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
0

Simply, No. Method signature is determined by its method name and the parameters accepted by it.

Nilaksha Perera
  • 715
  • 2
  • 12
  • 36
  • Not exactly true. There are two definitions (CLI and C#) and one includes return type and static modifier other not. – Andrey Jul 03 '15 at 10:49