C# Getting Started 1

Introduce simple variable types and basic program control structure

 

Simple variable type

note:

1. There is no upper limit on the number of characters of string type, using variable size memory

2. Assign a literal value to a string. You cannot assign a multi-line string. If you want to output in a new line, you need to add \n; for example, myString = "This string has a \nline break"

3. The string is a reference type, you can assign a null value, which means that the string (or other) is not quoted

4. You can use @ to specify a character string that does not change. That is, all characters in "" are included in the string, including the characters at the end of the line and the characters that originally need to be escaped.

The following two literal values ​​are equivalent: @"C:\temp\mydir" == "C:\\temp\\mydir"

5. Console.ReadKey(); used to wait for user input before the end of the program

6. The first character of the variable name must be a letter, underscore or @, note that it cannot be a number

7. Wrap output: Console.WriteLine

8. Output without line break: Console.Write

9. Without the Console static class name in front, the System.Console namespace needs to be included , and using static static inclusion is required; such as using static System.Console;

10. Use $ to format the output string, where {} is the variable name; as follows

Console.WriteLine($"The sum of {firstNum} + {secondNum} is {firstNum + secondNum}");

 

Operator precedence

 

Branch structure

if
...
else if 
... 
else


switch..case;

switch(test){
   case first:{
    ...
    break;
    }
    case second:{
    ...
    break;
    }
    ...
    default:...break;
}

note:

  1. In C#, switch...case cannot execute the remaining cases after executing one case. This is different from C/C++, but no statement is added after the case. Just stacking the cases together is equivalent to checking multiple conditions. Satisfying one of them will execute the following code.
  2. You can also use the goto statement to make one case jump to another after the execution, without adding break afterwards;

 

Loop structure

1、for(;;)

2、while()

3、do{
}while()

 

Guess you like

Origin blog.csdn.net/wyzworld/article/details/112496220