Declaration of methods in C # (four elements)
- Access modifier: Public,private(default access modifier for method)
- return value types: void and non-void
- Method Name: Specification is the method name takes the verb, the first letter of each word capitalized.
- Method Body: Writing logical statements
1. Say the access modifier, and say it again.
Public: Common, under the same namespace, any class can be accessed, under different namespaces, to access, add the corresponding namespace, and add references.
Internal: Can be accessed in the current assembly.
Protected: Protected, accessible in child classes of parent and parent classes.
Private: It can only be accessed in the current class.
2. Return value type:
Void means that there is no return value, you cannot use return to return a specific value, but you can use return to terminate the current method;
Non-void must return a value of the specified type. However, with two exceptions, ① returns a method of type double, which can return a value of type float, or int. The ② return value is a method of the parent class type that can return a value of its child class type.
3. The name of the method: usually verbs, the first letter of each word should be capitalized.
4 Method Body: Write logical statements.
Declaration of methods in C #