C#_ Basic _ Methods and method overloads (10)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.