nesh_s
nesh_s

Reputation: 399

Is access modifier part of Method Signature in C#?

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

Upvotes: 2

Views: 1640

Answers (3)

Andrey
Andrey

Reputation: 60085

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.

Upvotes: 4

Brian Mains
Brian Mains

Reputation: 50728

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>();

Upvotes: 0

Nilaksha Perera
Nilaksha Perera

Reputation: 725

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

Upvotes: 0

Related Questions