Methods implement the computations and actions that can be performed by a class. Methods are declared using method-declarations:
A method-declaration may include set of attributes (§17), a new
modifier (§10.2.2), a valid combination of the four access modifiers (§10.2.3), one of the static
(§10.5.2), virtual
(§10.5.3), override
(§10.5.4), or abstract
(§10.5.5) modifiers, and an extern
(§10.5.6) modifier.
The return-type of a method declaration specifies the type of the value computed and returned by the method. The return-type is void
if the method does not return a value.
The member-name specifies the name of the method. Unless the method is an explicit interface member implementation, the member-name is simply an identifier. For an explicit interface member implementation (§13.4.1) , the member-name consists of an interface-type followed by a ".
" and an identifier.
The optional formal-parameter-list specifies the parameters of the method (§10.5.1).
The return-type and each of the types referenced in the formal-parameter-list of a method must be at least as accessible as the method itself (§3.3.4).
For abstract
and extern
methods, the method-body consists simply of a semicolon. For all other methods, the method-body consists of a block which specifies the statements to execute when the method is invoked.
The name and the formal parameter list of method defines the signature (§3.4) of the method. Specifically, the signature of a method consists of its name and the number, modifiers, and types of its formal parameters. The return type is not part of a method’s signature, nor are the names of the formal parameters.
The name of a method must differ from the names of all other non-methods declared in the same class. In addition, the signature of a method must differ from the signatures of all other methods declared in the same class.