2016-12-30 C # Reading notes (3)

Source: Internet
Author: User

1. Conditional StatementsExecutes branch if (condition) {statement) according to whether the condition satisfies or controls the code based on the value of the expression;            } else {satement} If only one statement can omit {}, but it is recommended to put only one if and no else can have multiple else if, implement multiple branches: if (condition) { ...        } else if (condition) {...} else if (condition) {...}    else {...} else if the number of statements is not restricted the expression in the IF clause must be a Boolean value 2. Switch statementswitch (expression) {case constant 1: ... break;        CASE constant 2: ... break;        CASE constant 3: ... break; Default:.    Break     } case must be followed by a constant expression, not a variable.     The case clause must have a break (unlike C/java), with one exception, multiple cases being null: "A": Case ' B ': Case ' C ': break;        The case clause can be skipped through a goto to another instance, for example "a": "A" to "B";        Case ' B ': break;     Case ' C ': break;     The order of the case clauses does not matter the constant value after the case cannot be duplicated. The value of the test expression in switch can be an integer, string, enumeration, Boolean, character 3. For Loopfor (initialize; loop condition; iteration) {...}    Initialization is performed before the first loop, and only one loop condition is executed: Every time the loop is entered, it is used to determine whether to cycle. Iteration: Typically used as a counter to predict the number of cycles used for the loop for loops can be nested, the outer layer is an outer loop, inner loop inside. 4. While Loopwhile (loop condition) {...} 5 . Do ... while loopDo {...}     while (cyclic condition); Compared to while, do: While at least one loop is executed. 6. Foreach LoopYou can iterate over each item in the collection for (variable in collection) {...}    The Foreach loop iterates over one element at a time. The Foreach loop is read-only and you cannot change the values of the items in the collection.        such as: int[] arr = {1,2,3,4,5}; foreach (int i in arr) {i++;//Error} 7. Jump StatementThe goto statement can jump to the line specified by the label in the program, such as: Goto POSITION;        ...    POSITION://Definition tag: The identifier is followed by a colon ... goto cannot jump from outside to the for loop. Goto cannot jump out of the scope of a class Goto cannot jump out of the try ...     The finally block after the catch.     The break statement can be used in a switch, in a loop, to exit a switch or loop.     The continue statement can only be used for loops, and he exits the current loop for a next iteration. The return statement is used to exit the method, handing control over to the caller. 8. EnumerationEnumeration is a user-defined integer type declaration when you want to specify enumeration values that an instance of an enumeration can contain enumeration that inherits from the System.Enum class advantage: Enumerations can make code easier to maintain enumeration with names representing integer values, code clearly defined:        public enum Enum type name {value 1 = 0, value 2 = 1, value 3 = 2} using: enumeration. Value;     Enumeration variable = enumeration. Value;        The enumeration value is converted to a string: enumeration. Value. ToString (); Enumeration variables.     ToString (); String conversion to enumeration value: Enum variable = (enum type) Enum.parse (typeof (enum type), "string", True) the third parameter is: Do you want to ignore the case example:
namespacegame{ Public enumgamestatus {Start,playing,game_over}classProgram {Static voidMain (string[] args) {GameStatus Status=gamestatus.playing;        CheckStatus (status); }        Static voidcheckstatus (gamestatus status) {stringValue =NULL; Switch(status) { CaseGameStatus.START:value ="Game Start"; Break;  CaseGameStatus.PLAYING:value ="in-game running"; Break;  CaseGameStatus.GAME_OVER:value ="Game Over"; Break;        } Console.WriteLine (value); }    }}    

2016-12-30 C # Reading notes (3)

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.