Method: A mechanism that puts a piece of code together and makes repeated calls.
Grammar:
* [Private] static return value type function name (parameter list)
* {
* Function code;
*
* return value;
* }
*
* Public: Is the access modifier, which can be accessed publicly
* Static: Static
* Return value type: Void if no return value
* Method Name: First letter uppercase, remaining lowercase
* parameter list: The condition that this method must be provided to complete this method
* Return Effect: 1. End method; 2. Returns the value to return in the method
1 Exercise 1: Calculate the maximum value between two integers2 /// <summary>3 ///compares the size of two integers and returns a maximum value4 /// </summary>5 /// <param name= "NUM1" >integer</param>6 /// <param name= "num2" >integer</param>7 /// <returns></returns>8 Public Static intGetmax (intNUM1,intnum2)9 {Ten returnNum1 > num2?num1:num2; One } A - Exercise 2: Read the input integer, if the user entered a number is returned, otherwise prompt the user to re-enter - Public Static voidGetInt () the { - while(true) - { - strings =console.readline (); + Try - { + intnum =Convert.ToInt32 (s); A Console.WriteLine (num); at Break; - } - Catch - { -Console.WriteLine ("input error, re-enter"); - in } - } to Exercise 3: Judging whether it is a leap year + Public Static BOOLIsrun (intYear ) - { the BOOLB = (Year/ -==0) || (Year/4==0&& Year% -==0); * returnb; $}
Exercises
Method overloads:
Concept: Method name is the same, parameter list is different (parameter type, number of parameters)
1 ///compare two-digit maximum values2 Public Static intGetmax (intNUM1,intnum2)3 {4 returnNum1 > num2?num1:num2;5 }6 //three number of maximum values7 Public intGetmax (intNUM1,intNUM2,intnum3)8 {9 inttemp = NUM1 > num2?num1:num2;Ten returnTemp > num3?temp:num3; One}
C#_ Basic _ Methods and method overloads (10)