This is the ninth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
One way to do this is to group related statements together into blocks that execute a task. Every C# program has at least one class with a Main method.
The method call
1.1 Define method calls
Based on whether the method and the caller belong to the same C# class and whether the method has parameters, the method call can be divided into the following forms:
// Call the name of the invisible parameter method (argument list) // Call the name of the invisible parameter method (argument list) // call the name of the invisible parameter method (argument list) // call the name of the invisible parameter method (argument list)Copy the code
1.2 Named Parameters
In C#, when making a method call, the position of each argument must correspond to the position of the corresponding parameter. These arguments are called positional arguments.
Starting with C#4.0, a named parameter mechanism is supported for method calls. You can list the arguments in any order in a method call as long as you explicitly specify the names of the arguments.
Note when using named parameters:
- Method declarations are exactly the same as method declarations using positional arguments;
- When making a method call, the parameter name must be followed by a colon and the actual parameter value or expression,
Format is as follows:
Method name (parameter name: parameter value,... , parameter name: parameter value)Copy the code
In addition, when making a method call, you can use a mixture of positional and named arguments, but you must first list all the positional arguments.
Example:
class Program
{
static void Main(string[] args)
{
// Positional parameters
Sum(1.2.3);
// Named parameters
Sum(b: 1, c: 2, a: 3);
// Mixed use
Sum(1, b: 3, c: 2);
Console.ReadLine();
}
static void Sum(int a,int b,int c)
{
Console.WriteLine("a={0},b={1},c={2}", a, b, c); }}Copy the code
Named parameters are useful for self-describing programs. They help the programmer show which value was assigned to which parameter when making a method call, making method calls more informative and error-prone, and increasing the readability of program code.
Methods the recursive
- Recursive calls: Methods that call themselves directly or indirectly are called recursive calls.
- Recursion produces very elegant code.
- C# allows methods to implement both direct and indirect recursive calls.
A classic example of recursive method calls:
Rabbit breeding problems:
Have a pair of rabbits, from the 3rd month after being born, give birth to a pair of rabbits every month, little rabbit child grows to the 3rd month the month after giving birth to a pair of rabbits again, if the rabbit is not dead, ask the 1st several months ability to have 100 pairs of rabbits?
class Program
{
static void Main(string[] args)
{
int i = 10;
// End the condition by finding a larger number. The subsequent algorithm tests pass and can be true
while (i < 100)
{
if (Recursion(i) > 100)
{
Console.WriteLine("Rabbit reaches 100 pairs by {0} month.", i);
break;
}
i++;
}
Console.ReadLine();
}
static int Recursion(int n)
{
// Recursion exit: the first rabbit month and the second month are 1 pair
if (n == 1 || n == 2)
{
return 1;
}
else
{
// Recursion condition: the rabbits from the third month are the sum of the rabbits from the first two months
return Recursion(n - 1) + Recursion(n - 2); }}}Copy the code
Solve the problem recursively:
- Usually, a relatively large problem can be transformed into a smaller problem similar to the original problem to solve, and finally achieve the solution of the original problem.
- When you use recursion, you have to be careful that you have conditions that make the recursion end.
Method overloading
-
Definition: the phenomenon of more than one method in a C# class having the same name is called method overloading.
-
In C#, when implementing method overloading, you must ensure that every method using the same name has a different signature than any other method, and the compiler only looks at this aspect when determining whether method overloading is legal.
-
C# overload method condition: the method name is the same, the method parameter type or number of parameters is different.
Examples of incorrect overloading methods:
static void Sum(int a, int b){}static void Sum(int x, int y){}Copy the code
Examples of correct overloading methods:
static void Sum(int a, int b){}static void Sum(string a, int b){}static void Sum(int a, int b, int c){}Copy the code