. NET basis of 4--Process Control

Source: Internet
Author: User

(1) Exception capture

We often have a variety of anomalies in the program, if you want your program to become stronger.

You should use Try-catch frequently for exception trapping in your code.

Syntax: Try {    The Code of the exception may appear;}catch{The    code to execute after an exception occurs;} execution: If the code in the try does not have an exception, the code in the catch will not execute. If the code in the try has an exception, the code that follows does not execute and jumps directly into the catch statement to execute the code. 
1             //exception: There is no error in syntax and there is a problem due to some errors in the program. 2             BOOLb=true;3             intn=0;4Console.WriteLine ("Please enter a number:"); 5             Try6             {7n =Convert.ToInt32 (Console.ReadLine ());8                //Console.WriteLine (n * 2);9             }Ten             Catch One             { AConsole.WriteLine ("the input cannot be converted into numbers! "); -b=false; -             }      the  -             //we need to meet some conditions if we want to execute this line of code.  -             //Use the bool type if the code satisfies certain conditions to execute.  -             if(b) +             { -Console.WriteLine (n *2); +             } AConsole.readkey ();

(2) Switch-case structure

Used to deal with multiple conditions of the fixed value judgment.

Syntax: Switch (value of variable or expression) {    case  value 1: Code to execute;    break;      Case value 2: the code to execute;      Break ;      Case Value 3: the code to execute;      Break ;    .......     default : the code to execute;      Break ;} Execution procedure: The program executes to switch, first calculates the value in parentheses, and how to match the value following each case. When you encounter a break, jump out of the switch-catch structure.

(12) Breakpoint debugging

1, * program debugging

1), after the completion of a program, want to look at the implementation of this procedure.

2), when you finish writing this program, found that the program did not follow the way you think to do.

Debugging method:

1), F11-by-statement debugging (single-Step debugging)

2), F10 process debugging

3), breakpoint debugging

(20) Type conversion and method introduction

3, Int. TryParse

An attempt was made to convert a string to an int type.

 1  //  The conversion is done using convert, and it becomes a success and throws an exception if it fails.  2  // int Numberone = Convert.ToInt32 (" 123abc ");  3  4  //int. Parse is a built-in function of Convert.ToInt32.  5  int  number = int . Parse ( " 123   "  6   Console.WriteLine (number);  7  console.readkey (); 
1             int 0 ; 2             BOOL int. TryParse ("123" out number ); 3             Console.WriteLine (b);     // True 4             Console.WriteLine (number);  // 123 5             Console.readkey ();

(24) Three-dimensional expression

Grammar:

Expression 1? Expression 2: Expression 3;

1             //Generate random numbers2             //1. Create an object that generates random numbers3Random r =NewRandom ();4             //2. Let the object that generates the random number call the method to generate a random number5             intRnumber = R.next (1, One);//left closed right open interval6 Console.WriteLine (rnumber);7Console.readkey ();
1             //Enter a name for random output What was this man's last life?2Random r =NewRandom ();3              while(true)4             {5                 intRnumber = R.next (1,7);6Console.WriteLine ("Please enter a name:");7                 stringName =console.readline ();8                 Switch(Rnumber)9                 {Ten                      Case 1: OneConsole.WriteLine ("{0} Last life was a good man. ", name); A                          Break; -                      Case 2: -Console.WriteLine ("{0} The last life is a good person. ", name); the                          Break; -                      Case 3: -Console.WriteLine ("{0} Last life was a jolly fellow. ", name); -                          Break; +                      Case 4: -Console.WriteLine ("{0} Last life was a bad man. ", name); +                          Break; A                      Case 5: atConsole.WriteLine ("{0} The last life was a bad bad guy. ", name); -                          Break; -                      Case 6: -Console.WriteLine ("{0} Last life was a big bad man. ", name); -                          Break; -                 } in Console.readkey (); -}

. NET basis of 4--Process Control

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.