How to prepare for C #: Object-oriented <1> 〉,

Source: Internet
Author: User

How to prepare for C #: Object-oriented <1> 〉,
1. CLR load the compilation source file

 

Static void () {for (int I = 0; I <10; I ++) {for (int j = 0; j <10; j ++) {break; // exit the current loop continue; // exit the Console of this loop. writeLine ("not executed ");}}}

Note 1. the exit of the break indicates that the for loop is not executed no matter J is equal to 3 or 4.

Note 2. When continue releases this loop, it means that console. writeline under continue does not run this time and then executes the next loop where J is equal to 1.

5. Scope of variables (local variables)

1. Read the following program and explain the output result?

Public class _ 201201__02 the key condition for simple calculator {// method Overloading is that the method name is consistent and the parameter list is different; // method overloading, to complete the simple calculator static void Main4 (string [] arr) {// Request, receive two user numbers, of course, can be integer, it can also be a decimal number // output: Result of two numbers // prompt the user to enter the first number Console. writeLine ("Enter the first number:"); // accept the first number string strNumA = Console. readLine (); // prompt the user to enter the second number of consoles. writeLine ("enter the second number:"); // accept the second number string strNumB = Console. readLine (); // calculation result if (strNumA. indexOf ('. ')>-1 | strNumB. indexOf ('. ')>-1) {double fNumA = Convert. toDouble (strNumA); double fNumB = Convert. toDouble (strNumB); double iResult = Add (fNumA, fNumB); Console. writeLine ("Result:" + iResult);} else {int iNumA = Convert. toInt32 (strNumA); int iNumB = Convert. toInt32 (strNumB); int iResult = Add (iNumA, iNumB); Console. writeLine ("Result:" + iResult);} Console. readKey ();} // The following three methods have the same name, but the number or type of the parameter list of the method is different. Therefore, the method overload static int Add (int numA, int numB) {return numA + numB;} static double Add (double numA, double numB) {return numA + numB;} static double Add (double numA) {return numA ;}}

Note: the similarities and differences between out and ref are as follows.

Similarities: All parameters modified by out/ref are referenced.

Differences: 1. the out keyword focuses on the output parameter, that is, I call you to obtain the value through the output method. (Return value is also used to obtain the value through the method): it is usually used when the method requires multiple values. Therefore, the out parameter must be used when the method returns multiple values.

2. ref focuses on modification: that is, when I call your method, the input parameter is designed to modify the input variable in the method. Therefore, the ref parameter must be assigned a value before being passed in.

Sample Code:

Public class _ 201201__05atm {static void Main (string [] str) {double myMoney = 30000; Operating (ref myMoney) ;}# region 01. ATM start /// <summary> /// 01. ATM start // </summary> // <param name = "count"> account balance </param> static void Operating (ref double count) {do {Console. writeLine ("do you want to save or retrieve it? Dear user ~~ (1-save, 2-Fetch )~ "); String optType = Console. ReadLine (); if (optType! = "1" & optType! = "2") {Console. WriteLine ("the operation type you entered is incorrect. Please enter it again ~~ "); Continue;} do {Console. writeLine ("Enter the amount to be operated (in Yuan):"); string strMoney = Console. readLine (); double dMoney = 0; if (double. tryParse (strMoney, out dMoney) // If the conversion is successful, true is returned, and the value in dMoney has been assigned. {// Input parameter: operation type, operation amount, account balance if (Branch (optType, dMoney, ref count) {Console. WriteLine ("Operation completed, succeeded ~~ "); Console. writeLine ("show balance:" + count); break; // If any error occurs during the operation, the current cycle is exceeded (this cycle is used to prompt the user to re-operate when there is a data type error)} else {Console. writeLine ("the data type you entered is incorrect ~~ ") ;}} While (true); Console. WriteLine (" do you want to continue? (Y/N) "); string strRes = Console. readLine (); if (strRes. toUpper () = "N") {break; // jumps out of the entire operation cycle and the task is about to end} while (true); Console. writeLine ("task end ~~ Press any key to exit "); Console. readLine () ;}# endregion # region 02. determine the operation by type /// <summary> /// 02. operation Based on type judgment /// </summary> /// <param name = "optType"> </param> static bool Branch (string optType, double money, ref double count) {switch (optType) {case "1": SaveMoney (money, ref count); return true; case "2": {return GetMoney (money, ref count);} default: return false ;}# endregion # region 03. save and withdraw static void SaveM Oney (double money, ref double count) {count + = money; Console. WriteLine ("saved successfully ~~ ");} Static bool GetMoney (double money, ref double count) {if (money <count) {count-= money; Console. WriteLine (" money Acquisition succeeded ~~ ");} Else {Console. WriteLine (" Sorry, your balance seems to be insufficient ~~~ ");} Return money <count ;}# endregion}

 

7. Variable Parameter Params

Sample Code:

Public class _ 201201__06 variable array parameter {static void Main8 (string [] str) {// 1. Variable Parameter params: when used (when calling the method): // 1. you can also directly upload the string [] dogsNames = {"Xiaohua", "niu", "a yellow", "rhubarb", "Xiaoqiang"} into the array "}; // BuyDogs (dogsNames); // 2. you can also directly transfer the elements of the same type as the parameter array. // BuyDogs ("Xiaohua", "niu", "a yellow", "rhubarb", "Xiaoqiang "); // 2. Easy-to-make errors // specify variable parameters in the parameter list of a method: // 1. there can be only one variable parameter in the parameter list of the method; // 2. the only variable parameter must be at the end of the parameter list; BuyDogs ("Liu Dehua", "Xiaohua", "niu", "A Huang", "rhubarb", "Xiaoqiang "); console. readLine ();} static void BuyDogs (params string [] dogsNameArr) {for (int I = 0; I <dogsNameArr. length; I ++) {Console. writeLine ("no." + (I + 1) + "Puppy name:" + dogsNameArr [I]) ;}} static void BuyDogs (string masterName, params string [] dogsNameArr) {for (int I = 0; I <dogsNameArr. length; I ++) {Console. writeLine (masterName + "" + (I + 1) + "Puppy name:" + dogsNameArr [I]);} static void BuyDogs (string masterName, string masterName2, params string [] dogsNameArr) {for (int I = 0; I <dogsNameArr. length; I ++) {Console. writeLine (masterName + ", masterName2 =" + masterName2 + "" + (I + 1) + "the name of a puppy is: "+ dogsNameArr [I]) ;}}

Note: When the parameter lists of multiple methods are the same as those of variable parameters, you call this method first.

 

-- This article is a more in-depth introduction for future studies, such as the out and ref in-depth studies.

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.