CTRL+K CTRL+C; (I don’t know what the designer thought, stupid)

VS uncomment: CTRL+K CTRL+U; (stupid dead)

CTRL+A CTRL+K CTRL+F; (stupid dead)

PS: Finally couldn’t bear to change VS Code 😂😂😂😂😂

1. If statements

            if(Condition 1) {execute 1; }else if(Condition 2) {execute 2; }else{other execution; }Copy the code
If the execution statement has only one sentence, you can also abbreviate it:

            if(Condition 1) Perform 1 (only one sentence);else if(Condition 2) Perform 2 (only one sentence);else(Condition 3) other execution;Copy the code

2. A switch statement

switch (op) {
                case "+":
                    op = "a+b";
                    break;
                case "-":
                    op = "a-b";
                    break;
                case "*":
                case "×":
                    op = "a*b";
                    break;
                default:
                    op = "";
                    break;
            }Copy the code
The switch variable can be an integer string Boolean char enumeration or a type that can be null, but nothing else

Case can only judge a fixed value;

Each case must break, including default;

Multiple cases executing the same statement can be abbreviated;

3. While statement

while (true) {
                
                break; //continue;return;breakAdd equal jump statement; };Copy the code

4. The for loop

            for(int i = 0; i < 10; I++) {execute statement; }Copy the code

4.1 Foreach Traversal function

foreach (int i in arr){

}Copy the code

4.2 Some methods of string

            string str = "My name is Annie.";
            Console.WriteLine(str.IndexOf("Ann"));
            str.StartsWith("我"); // Determine the leading character, return Boolean; string[] arr; arr = str.Split("Call"); // Returns a string group with a character cut string; str = str.Insert(2,"Small"); // Insert the character before the second; Console.WriteLine(str); str = str.Replace("Small"."Big"); // Replace old characters with new characters; Console.WriteLine(str); str = str.Remove(2, 1); // Remove a console. WriteLine(STR) from the second; str = str.Remove(2); // Remove the Console.WriteLine(STR) from the second;Copy the code

5. Method (function) structure

Private static return value method name (parameter) {

Logical statement;

Return Return value; Return (void); return (void); return (void);

}

private static int getWeekByDay(int year, int month, int day)
        {
            DateTime dt = new DateTime(year, month, day);
            return (int)dt.DayOfWeek;
        }Copy the code

6. Overloading methods (functions)

Methods with the same name, return the same value, but with different parameters are treated as different methods, and can be overloaded by passing in different parameters when called

Example:

        private static long CountSec(int min) {
            return min * 60;
        }
        private static long CountSec(int hour,int min)
        {
            return hour*CountSec(60)+min * 60;
        }
        private static long CountSec(int day,int hour, int min)
        {
            returnDay * CountSec(24,0) + hour * CountSec(60) + min * 60; }Copy the code

7. Recursion

Recursion advantage: logic simplification

Disadvantages of recursion: Beware of stack overflow, poor performance

Example: Recursive factorial: 5*4*3*2*1

s3*2*1tatic void Main(string[] args)
        {
            Console.WriteLine (DiGui(5));
        }
private static int DiGui(int num) {
            if (num == 1) return 1;
            return num * DiGui(num - 1);
        }Copy the code

An array of 8.

Arrays must be of the same data type

Arrays are written as follows:

① Declaration, initialization, assignment

int[] a; // Declare array a = new int[3]; // Initialize array a[2] = 2; // Array assignmentCopy the code
② Initialization plus assignment

Int [] arr = new int[5] {1,2,3,4,5};Copy the code
③ Declaration + initialization + assignment

Int [] arr = {1,2,3,4,5};Copy the code
④ Anonymous array

,53,64,77,88 GetMax (new int [] {12})Copy the code

9. Var inferred types

Infer the type from the assigned data

10. Declare the superclass type assignment subclass object

Example: Declare the parent class, pass the parameter pass any subclass can be.

Private static void fun1(Array arr) {execute statement; }Copy the code

Random number 11.

static Random random = new Random(); // This sentence is written outside the method, inside the class.

random.Next(1, 34); // include the head but not the tail;

12. Array methods

① Get the array length

An array of Length

② Clear elements

Attention!! Clear is not delete. Clear simply clears the value to the default !!!!

Array.Clear(Array, start position, Clear length);

Example: array. Clear(arr, 2, 4);

③ Array copy elements

(1) array. Copy(source Array, source Array start index, target Array, target Array start index, Copy length);

int[] arr = { 1, 4, 2, 6, 7, 9 }; int[] arr2 = { 0, 0, 0, 0, 0, 0 }; Array. Copy (arr2 arr, 2, 2, 3);Copy the code
(2) array. Copy(source Array, destination Array,, Copy length); // Assign copies from the first one

Copy the source array into the target array starting at index

Source array.CopyTo(target array, start index of target array);

Arr. CopyTo (arr2, 0);

⑤ Array cloning

arr2=(int[])arr.Clone(); // Because Clone returns Object;

⑥ Find elements

Array.indexof (Array, element) // Returns the element index if there is no such element, returns -1;

Array.IndexOf (arr,6)

Array.LastIndexOf (arr,2)

⑦ Array sort

Array.Sort (arr); // No return value

Array inversion

Array.Reverse (arr);

13. Two-dimensional arrays

Int [and] a = new int [5, 3];

Can also:

Int [,] arr={{1,2,3}, {4,5,6}};

① A two-dimensional array can also be traversed

foreach(int each in arr){
 Console.WriteLine (each);
            }Copy the code
② Get the number of rows and columns in the array

Console.WriteLine(arr.GetLength(0)); / / lines of the Console. WriteLine (arr. GetLength (1)); / / the number of columnsCopy the code

14. Staggered arrays

int[][] arr; arr=new int[4][]; Arr [0] = new int [] {1, 2, 3}; Arr [1] = new int [] {4, 5}; arr[2]=new int[]{6}; Arr [3] = new int [] {7,8,9,10}; Arr [4] = new int [] 11, 12} {;Copy the code
Each element of a jagged array is an array

(2) Interleaved arrays can also be foreach traversal // double traversal;

int[][] arr; arr=new int[4][]; Arr [0] = new int [] {1, 2, 3}; Arr [1] = new int [] {4, 5}; arr[2]=new int[]{6}; Arr [3] = new int [] {7,8,9,10}; arr[0][0]=6; foreach(int[] eachArrin arr){
                foreach(int each in eachArr){
                    Console.Write(each+"\t");
                }
                Console.WriteLine();
            }Copy the code
Attention!!!!!! Each element of a jagged array is an array so the for loop should

for (int i = 0; i < arr.Length; i++)
            {
                for (int j = 0; j < arr[i].Length; j++)
                {
                    Console.Write(arr[i][j] + "\t");
                }
                Console.WriteLine();
            }Copy the code

15. Parameter array

When the type of arguments is certain but the number is uncertain, an array is appropriate

Parameter array:

private static int Sum(params int[] arr){}

When sending parameters, you can:

sum();

The Sum (new int [] {1, 2, 3});

The Sum (1, 2, 3);

Summary of parameter array features:

① Inside the method, the argument array is just an ordinary array.

2. Pass array. Pass variable count. 3