Reputation: 10395
There are several questions on Stack Overflow about a method signature in C#. Almost all solutions say that a return value and access level are not parts of a method signature. But I found on MSDN completely opposite information.
Methods are declared in a class or struct by specifying the access level such as public or private, optional modifiers such as abstract or sealed, the return value, the name of the method, and any method parameters. These parts together are the signature of the method.
and
A return type of a method is not part of the signature of the method for the purposes of method overloading. However, it is part of the signature of the method when determining the compatibility between a delegate and the method that it points to.
I'm confused. What is the truth?
Upvotes: 0
Views: 2343
Reputation: 659984
The C# specification defines the signature as not containing the return type. The CLI specification defines it as containing the return type.
Though this small discrepancy is unfortunate, it is not difficult to reason from context which meaning is intended. As the text you quoted indicates, the return type is relevant when matching methods to delegates; it is also relevant when doing virtual overrides, and in a number of other situations. The return type is not relevant during overload resolution, which is the context in which the C# specification is most concerned about the "signature" of a method.
Upvotes: 7
Reputation: 6413
They just don't use term 'signature' in very strict sense. The first use is strict, the second use is not. What they want to emphasize is that for delegates both signature and return type are important.
Note that the MSDN is a guide, not specification. Sometimes they do some simplifications or more loose terminology.
Upvotes: 4
Reputation: 8699
Per Section 3.6 of the C# language specification, the method signature does not include the return type (explicit) or the access modifier (by omission):
The signature of a method consists of the name of the method, the number of type parameters and the type and kind (value, reference, or output) of each of its formal parameters, considered in the order left to right. For these purposes, any type parameter of the method that occurs in the type of a formal parameter is identified not by its name, but by its ordinal position in the type argument list of the method. The signature of a method specifically does not include the return type, the params modifier that may be specified for the right-most parameter, nor the optional type parameter constraints.
Upvotes: 5