C # knowledge point,
C # Cross-language and java cross-platform
C # is an object-oriented language
Three basic features of object-oriented architecture:
1. Encapsulation
2. Inheritance
3. Polymorphism
Output line breaks
Console. write ();
Output line feed
Console. writeLine ();
Non-printable characters
Space
\ T tabulation
\ N line feed
\ 'Single quotes
\ "Double quotation marks
Data Type
1. Value Type
Integer: short, int, long
Float: float (single precision) and double (double Precision)
Boolean Type: bool
Enumeration: enum
2. Reference Type
Character Type: char, string
Class: class
Interface: interface
Array: []
Object
Note:
The default decimal point is double. If the float type is used, add f after the decimal point.
Bool has only two values: true and false
The char value must use '', and the string value must use ""
Variable name naming rules:
Other letters
Letter _ letter _ number
For example:
Age
Other letters
A ge
Variable
Definition:
A bucket whose internal values can be changed
Features:
The value of the bucket can be changed by value assignment.
For example:
Int age = 10; // value assigned when declaring
Age = 11; // assign a value again
The last age value is 11.
Syntax:
One statement at a time:
Data type variable name;
For example:
1) string name;
2) int age;
Multiple declarations of the same type at a time:
Data type variable name 1, variable name 2;
For example:
1) string name1, name2;
2) int num1, num2, num3;
Assignment:
1. assign values when declaring
For example:
1) string name = "ccliu ";
2) int num1 = 15, num2 = 30;
2. Declare and assign values
For example:
1) string name; name = "ccliu ";
2) int num1, num2;
Num1 = 15; num2 = 10; // values are not the same
Num1 = num2 = 10; // same value assignment
Constant
Definition:
Storage space whose internal values cannot be changed
Features:
After the bucket is declared as a value, the value cannot be changed.
Syntax:
Const data type name = value;
For example:
Const double PI = 3.1415;
Note:
1) The constant name must be capitalized.
2) constants must be assigned values during declaration.
3) The constant value cannot be changed.
Input:
Function: Console. ReadLine () is used to receive a line of strings entered by users on the Console.
Syntax:
Console. ReadLine ();
For example:
The syntax for receiving string types is as follows:
String name = Console. ReadLine ();
The syntax for receiving integer types is as follows:
Int age = Convert. ToInt32 (Console. ReadLine ());
Receive Double Precision Floating Point
Double money = Convert. ToDouble (Console. ReadLine ());
Type conversion:
1. Display conversion (forced conversion)
1) Convert. ToXXX (any data type); // the first type is recommended.
2) XXX. Parse (string data );
Int money = (int) 3.14;
Int money = Convert. ToInt32 (3.14 );
Int money = int. Parse (3.14 + "");
2. implicit conversion (default conversion)
Char short int long float double
Note:
1) char cannot be implicitly converted to short
2) implicit conversion from left to right (default conversion)
3) display conversion from right to left (forced conversion)
Operator
()
RMB 1
++ --! (Non-logical)
Binary
Computing
First */% +-
Off
First >>=<=then =! =
Bytes
First & (logical and) | (logical or)
Assign
First * =/= % = + =-=
Sanyuan
Expression? Value 1: Value 2
Receive user input:
// Receives user input numbers
Int num = Convert. ToInt32 (Console. ReadLine ());
// Receives decimals from users
Double money = Convert. ToDouble (Console. ReadLine ());
// Receives user input strings
String name = Console. ReadLine ();
// Receives user input characters
Char ch = Convert. ToChar (Console. ReadLine ());
Three basic process control structures
1. Order
Features: The program will execute all statements in order of writing.
2. Conditions
Features: the execution process of a program is determined based on conditions.
Category:
1) Judgment
If-else
2) Branch
Switch-case
3. Loop
Feature: execute a program segment repeatedly when a given condition is set until the condition is invalid.
Category:
1) while loop
2) do-while loop
3) for Loop
Analysis:
Determine if-else
Category:
1) simple if
Usage:
Determine a condition and execute it when the condition is set. Otherwise, nothing will be done.
Syntax:
If (expression)
{
Statement block;
}
Expressions include:
Bool type value/Relational Expression/logical expression
Resolution:
When the expression value is true, the statement block {} after if is executed.
For example:
If (true) if (1 = 1) if (1 = 1) | false)
{{{
}}}
2) General if
Usage:
Determine a condition. If the condition is true, an operation is performed. Otherwise, another operation is performed.
Syntax:
If (expression)
{
Statement Block 1;
}
Else
{
Statement Block 2;
}
Expressions include:
Bool type value/Relational Expression/logical expression
Resolution:
When the expression value is true, the statement Block 1 in {} after the if statement is executed
When the expression value is false, the statement Block 2 in {} After else is executed
3) Multiple if
Usage:
Perform different operations on multiple judgments of one condition
Syntax:
If (expression 1)
{
Statement Block 1;
} Else if (expression 2)
{
Statement Block 2;
} Else if (expression 3)
{
Statement block 3;
} Else
{
Statement block 4;
}
Expressions include:
Bool type value/Relational Expression/logical expression
Resolution:
When expression 1 is true, statement Block 1 is executed.
When expression 2 is true, statement Block 2 is executed.
When expression 3 is true, statement block 3 is executed.
Otherwise, execute Statement 4.
4) nested if
Usage:
When a condition is set up, the execution of the next condition judgment is affected.
Syntax:
If (expression 1)
{
If (expression 2)
{
Statement Block 2;
}
} Else
{
Statement block 3;
}
Expressions include:
Bool type value/Relational Expression/logical expression
Resolution:
When expression 1 is true, expression 2 is judged.
Otherwise, execute Statement 3.
Switch Structure
Role: Multi-Channel judgment on a condition
Syntax:
Switch (expression) // The expression value can be a char, int, or string type variable.
{
Case constant 1:
Statement 1;
Break;
Case constant 2:
Statement 2;
Break;
Default:
Statement 3;
Break;
}
Execution Process:
Compare the expression values in the switch statement with the constant expression after the case statement in sequence. If a match exists, execute the statement in the current case until break is executed, and then exit the switch structure.
If no match exists, the default statement is executed. If no default exists, the program is interrupted.
Note:
1. After the case and default statements, the break cannot be omitted if there is only one line of statements.
2. When the case statement does not have any statement, break can be omitted
3. The order of case and default statements can be changed.
The similarities and differences between switch and multi-if are as follows:
Similarities:
The two processes are very similar. They all start the next condition judgment when the previous condition is not true.
Differences:
The switch structure is only applicable to the Equality judgment of integer, string, and string types, and has some limitations.
The multi-if structure is more useful when determining conditions. It is mainly used when conditions are determined as intervals.
-------------------------------------------------------------
Array
Definition: An array is a continuous storage space in memory.
Purpose: stores a set of data of the same data type.
Declare the space allocation Syntax:
1. Explain the space allocation syntax first:
Data Type [] array name; // declaration syntax
Array name = new data type [size]; // allocate space
Emg:
String [] names; // declares an array of the string type.
Names = new string [5]; // allocate 5 spaces to the names array of the string type
2. syntax for space allocation during declaration:
Data Type [] array name = new data type [size];
Emg:
Int [] nums = new int [5]; // declare an array of the int type nums and allocate 5 spaces to it
Assignment Syntax:
1. assign values when declaring
Int [] nums = new int [3] {1, 2, 3 };
Char [] chs = new char [] {1, 2, 3, 4 };
Double [] moneys = {12.3, 23.4, 34.5 };
2. Declare first and then assign values cyclically
Reference Cases
Features:
------------------------------------------
Loop
Definition: execute a program repeatedly when a given condition is set until the condition is invalid.
Loop components:
1. Cyclic condition Initialization
2. cycle end conditions
3. Loop body
4. Modify the cyclic condition variable
Category:
1. while Loop
2. do-while loop
3. for Loop
4. foreach Loop
While Loop
Features: first judgment and then execution
Syntax:
① Cyclic condition initialization;
While (② loop end condition)
{
③ Cyclic body;
④ Modify the cyclic condition variable;
}
Execution Process
Execute ①,
Determine whether or not ② is true
When ② is established, it is executed until ③ ④
Otherwise, the cycle ends.
Do-while loop
Features: Execute the command first and then judge
Syntax:
① Cyclic condition initialization;
Do
{
③ Cyclic body;
④ Modify the cyclic condition variable;
} While (② loop end condition );
Execution Process
Execute ① ④
Determine whether or not ② is true
When ② is established, it is executed until ③ ④
Otherwise, the cycle ends.
For Loop
Features: first judgment and then execution
Syntax:
For (① cyclic condition initialization; ② cyclic end condition; ④ modifying cyclic condition variables)
{
③ Cyclic body;
}
Execution Process
Execute ①,
Determine whether or not ② is true
When ② is established, it is executed until ③ ④
Otherwise, the cycle ends.
----------------------------------------------------
Break
Function: Used to exit or end a code block.
Syntax:
Break;
Usage:
1. apply to switch-case
Purpose: Jump out of switch-case and execute the code
2. apply to Loops
Purpose: end the loop and then run the code after the loop is executed.
Continue
Purpose: Terminate this operation and continue the next operation.
Syntax:
Continue;
Usage:
1. apply to Loops
Purpose: Terminate this cycle and continue the next cycle.
Note: break and continue are usually combined with if judgment.
Return
Purpose: Return
Syntax:
Return value or expression;
Usage:
1. apply to functions (methods)
Purpose: return a specific value.
-------------------------------------------------------
Branch judgment
Switch (char/int/string type variable)
{
Case constant 1:
Statement Block 1;
Break;
Case constant 2:
Statement Block 2;
Break;
Default:
Statement block;
Break;
}
Condition judgment
Simple if
If ()
{
}
General if
If ()
{
} Else
{
}
Multiple if
If ()
{
} Else if ()
{
} Else if ()
{
} Else
{
}
Nested if
If ()
{
If ()
{
}
}
Switch is used for equivalent judgment. Generally, it is used for three or more equivalent judgments.
Multiple if values are used for equivalent judgment or range judgment. Generally, they are used for less than three times.