C # Getting Started knowledge,
[Concept]
First of all, let's talk about what is a method (function), what is its return result, and what are the benefits of using a method (function.
A function is a mechanism for reusing a bunch of code. A function is a piece of code. This code may have input values (parameters) and may return values. A function is like a person who specializes in this task. We call it to do some things. It may require us to provide some data for it. After it is executed, some execution results will be given to us. The required data is called a parameter, and the returned execution result is the return value.
With function writing, code is like building blocks. Various technologies in C # actually organize different functions according to certain logic through basic syntaxes such as for and if.
[How to define a method]
Next, let's take a look at how to define a method and how to pay attention when naming or calling it.
Definition method:
[Access modifier] [static] Method Name of Return Value Type ()
{
Method body;
}
Naming rules: method names start with uppercase, parameter names start with lowercase, and parameter names and variable names must be meaningful.
Method call: for static methods, there are two methods to call-> if you are in the same class, you can directly write the name to call it, or the class name. Method Name ();
Return can exit immediately
[Example]
The following two methods are used as examples: no return value, no parameter, no return value, and no parameter.
// No return value, no parameter show (); console. readkey ();} public static void show () {console. writeline ("Enter the first number"); int num1 = convert. toint32 (console. readline (); console. witeline ("enter the second number"); int num2 = convert. toint32 (console. readline (); int number = num1> num2num1: num2; console. writrline ("maximum value: {0}", number );}
// Return value and console parameter. writeline ("Enter the first number"); int number1 = convert. toint32 (console. readline (); console. witeline ("enter the second number"); int number2 = convert. toint32 (console. readline (); show (number1, number2); console. readkey ();} public static void show () {int number = num1> num2num1: num2; return number ;}
[Method overloading and rewriting]
In simple terms, the method is overloaded with the same method name, but different parameters or the number of parameters varies by type.
Method overload: there is A method A in A class, and you create Method B in this class. The name of Method B is the same as that of method A, and the return value type is the same, but the type or number of parameters are different. In this case, B reloads.
For example:
public class TestClass{ public int test(int i){return 1;} public int test(float f){return 1;}}
Method Rewriting: one class M inherits from another class N, and N has A method A. At this time, you write Method B in M, and the name, return value, and parameter of Method B are the same as those of method, in this case, B overwrites.
For example:
public class TestClass1{ public int test(int i) { return 1; }}public class TestClass2 extends TestClass1{ public int test(int i) { return 2; }}
[Summary]
The above is a summary of the knowledge point of the method. If you cannot accurately point out the knowledge point, please correct it.