A method is a member of a class that is used to perform calculations or other behaviors. Let's look at the declaration format of the method:
Method-header Method-body
The format of the method header Method-header:
Attributes Method-modifiers Return-type member-name (formal-parameter-list)
The parameters passed to the method are declared in the formal parameter table Formal-parameter-list of the method, which we will discuss in detail later.
In the declaration of a method, at least the method name, modifier, and parameter type should be included, and the return value and parameter names are not required.
Note: The method name Member-name should not have the same name as the other methods in the same class or the other member names in the class.
Modifiers
The modifier method-modifier of the method can be:
New
Public
Protected
Internal
Private
Static
Virtual
Sealed
Override
Abstract
extern
For methods that use the abstract and extern modifiers, the execution body of the method method-body only a simple semicolon. All other method bodies should contain the statements to be executed to invoke the method.
return value
The type of the return value of a method can be a valid data type for C #. C # Gets the return value in the execution part of the method, such as:
Program Listing 11-1:
Using System;
Class Test
{public
int max (int x,int y) {
if (x>y) return
x;
else return
y;
}
public void Main () {
Console.WriteLine ("The max of 6 ADN 8 is:{0}", Max (6,8));
}
The output of the program is:
The max of 6 and 8 Is:8
If you do not follow any value after return, the method returns the value is void.