02C # BASICS (1 ),
1. Keywords
Many keywords are defined in C #. Keywords constitute the basic syntax of C #. You don't need to back them up. Remember the keywords when you use them.
2. identifier
Identifiers are used to name classes, methods, and variables.
Naming rules:
(1) It consists of letters, Chinese characters (not recommended), numbers, and underscores;
(2) It cannot start with a number;
(3) cannot be a keyword
C # The language is case sensitive: the demo and Demo are two identifiers.
Camper name: the first letter of each word is capitalized;
Identifiers must be meaningful. The naming conventions of identifiers are not mandatory, but are "Hidden Rules": Class names and method names start with uppercase; variable names start with lowercase letters
3. Variables
Defines the format of a variable: variable name = initial value;
Variable Scope
A variable can only be declared once in one scope;
Variable assignment
Before using local variables, you must assign an initial value;
4. numeric type and explicit implicit conversion
Integer type:
Value range of bytes occupied by Data Types
Byte (byte) 1 0 to 255
Short (short integer) 2-2 ^ 15 to 2 ^ 15-1
Int (integer) 4-2 ^ 31 to 2 ^ 31-1
Long (long March) 8-2 ^ 63 to 2 ^ 63-1
Value range: long> int> short> byte
Decimal type
Value range of bytes occupied by Data Types
Float (single-precision floating point number) 4 do not remember
Double (double-precision floating-point number) 8
Value Range: double> float
If the declared variable value is out of the value range, a compilation error occurs;
Type conversion
When a variable or constant with a small value range is assigned to a variable with a large value range, this is when a variable with a large range of implicit type conversion is assigned to a small variable, no matter what it is, an error is reported (the type with a large range cannot be implicitly converted to a type with a small range. In this case, you need to use explicit (forced) type conversion to tell the compiler: Right, the type to be converted will not exceed the range
Escape Character
\ N press enter \ t tabulation \ "quotation marks \ is to tell the compiler" \ "after the n has a special meaning, need to be converted to a special character
If a string has multiple characters to be escaped, you can add "@" before the string. For example: @ "c: \ windows \ system \ cmd.txt ", convert all '\' of the string to true '\'.
Char type
The char type has only one character;
Bool type
Only valid values: true and false.
Convert the numeric type to the string Convert. ToString (numeric type to be converted );
Convert string to int type Convert. ToInt32 ("string to be converted ");
5. CTS (Common Type System) General Data Type System
. Net defines common data types: String, Int16, Int32, Int64, Boolean, and Double;
By going to the definition, we can see that these are all classes (struct );
C # The original types such as string, int, long, bool, and doubl are defined in the language specification. the compiler translates these types into the classes in CTS.
6. Enumeration type
Enumeration is a special type syntax that defines the value range:
Statement
1 enum Dir2 {3 East, West, South, North4} 5 6 // use 7 Dir d = Dir. East;
7. Basic Operators
(1) The operator is the addition, subtraction, multiplication, division, and other symbols: + ,-,*,/;
(2) returns the remainder operator "%" 5% 4 = 1 5% 5 = 0
(3) Auto-increment: ++ is an auto-increment operation for a variable.
Auto-subtraction: auto-Subtraction
(4) + you can splice strings with "123" + "456" = "123456"
(5) + can be spliced with other types
(6) Question: What is the result of 3 + 5 + "hello" + 5 + 3. 8hello53
8. Value assignment operator "="
Int I = 5;
It should be read to declare the int type variable I and assign 5 to I;
9. Value Type assignment
The value type (numeric type, boolean, char, enumeration, struct, etc.) is a copy transfer;
1 // how do I exchange the values of two int-type variables? 2 int I = 10; 3 int j = 20; 4 int temp = I; // define a temporary intermediate variable and hand over the I value to temp 5 I = j; // give the value of j to I. At this time, the value of I is stored in temp for 6 j = temp; // j obtains the value of I in temp, in this way, the exchange is successful 7 8 9 // think about it: why can the following code exchange values between the two without any intermediate variables? 10 int I = 3; 11 int j = 5; 12 I = I + j; // The sum of the connected variables is 13 j = I-j; // j obtains the sum minus j's own value, which is the value of I 14 I = I-j; // The value of j is already 3, I get the sum minus j = 3, which is the original j value.
The principle is the definition of addition and subtraction in elementary school:
Addition + addition = and;
The sum of the two numbers minus the addition of another known number is equivalent to the subtraction-subtraction = difference;