------------------------------------------------
Key Tips:
1, the basic data type difference: char, BYTE, short, int, long;float, double;
2. What is CTS? The difference between string and string;
3, enumeration type;
4, self-increase self-reduction;
5, how to exchange two variables;
6, the value of the assignment expression: Console.WriteLine (a=b+1), the assignment expression also has a value, its value is the value of the left after copying;
7, ternary operator: String s= (i==5? ") Hello ":" I am Good ");
8. If;if......else;if......else If ... ; case: BMI calculator;
9, Switch......case;
------------------------------------------------
Section 1th keywords and identifiers
C # defines some keywords (public/static/void/class/int/string) that are used to form the basic syntax of C #. (Default blue)
The Main, String, console, and so on are not keywords. (Default line green)
identifiers are used to name classes, methods, variables, and so on.
Naming rules: 1) consists of letters, Chinese (not recommended), numerals, underscores;
2) cannot begin with a number;
3) cannot be a keyword.
The C # language is case-sensitive : Demo and demo are two things;
Hump naming: Capitalize the first letter of each word. If it is a variable, the first letter of the initial word should be lowercase
identifiers should be meaningful;
The naming convention for identifiers is not mandatory, but "unspoken rules":
Class name, method name: uppercase Start ;
Variable name: lowercase start .
Chinese can do variable names, not recommended.
Special symbols cannot form identifiers.
(*) Extension: Some students mentioned that "C # can also use @ do identifiers,
For example, int @a=5;int @int = 6; ", in fact, @ is just an escape character, not part of an identifier.
Special symbols cannot form identifiers.
------------------------------------------------
Section 2nd Declaration and assignment of variables, scope of variables
The data used by the program is generally in memory. To manipulate memory, use the address, manipulate the memory by pointing to the memory pointer,
A variable is a name that is accessed within this paragraph. I'm going to tell someone how to use this memory. You need to give this area a name, such as Price, which is the variable name "price".
Variables and data are two of things.
DECLARE + Assign initial value variable type variable name = initial (match type)
Declaration: Variable type variable name;
Assignment: Variable name = initial value;
The initial value must be assigned before the change is used.
Scope of a variable: A variable can be declared only once within the same scope.
------------------------------------------------
Section 3rd Introduction to numeric types and explicit implicit conversions
Numeric type:
Integer type:
Type occupies byte value range
Byte (bytes) 1 0 to 255 unsigned (smallest unit of memory)
Short (shortened) 2-2^15 to 2^15-1
int (integral type) 4-2^31 to 2^31-1
Long (full) 8-2^63 to 2^63-1
Decimal type:
Float (single-precision floating point) 4 don't remember.
Double (dual precision floating point) 8 don't remember.
Long > int >short >byte
Double > float
1, byte is a "byte" type, which represents a 8-bit binary, that is, a byte.
2, the integer constant is the int type by default , and the decimal constant is the double type.
3, choose the data type when considering the possible range of data, select the minimum range of types, so as to save Resources .
Note: Do not use Chinese and special characters for project naming.
Hyper-Range: compilation error, unable to generate EXE.
Type conversions: (compilation only looks at the type, not the actual value)
Small-range conversions to large ranges are implicit type conversions
Large range conversion to small range must be explicit type conversions, possibly data loss (type)
------------------------------------------------
4th. Issues such as the declaration type conversion of float and double
The decimal type is doubleby default, and F (f) must be added when declaring a float type.
Note: When displaying data transformations, be aware of data loss issues.
1. Statement
Double D1 = 3.14d;//ok,3.14d can also
Double D2 = 3.14;//ok, generally do not write "D"
Double D3 = 3;//ok
Float F1 = 3.14;//error: Cannot convert from double to float. Decimal is double by default
float F2 = 3.14f;//ok,3.14f can also
2, Decimal type conversion
Double D1 = 3.14;
float f1 = 3.14F;
Double D2 = F1;//ok, implicit type conversion
Float F2=d1;//error: Cannot convert from double to float
float f3= (float) D1;//ok, explicit type conversion
int i1 = D1;//error
int i2 = (int) D1;//ok, explicit type conversion, loss of decimal part
Double D3 = I1;//ok, implicit type conversion
3, plus (+) minus (-) multiply (*) divide (/)
Integer/integer result is an integer
Operations with fractional participation, resulting in decimal type (plus f)
Calculate 5 divided by 8:
Console.WriteLine (5/8); result:0 because both sides are int the result is implicitly converted to an int (missing decimal part)
Console.WriteLine (5.0/8.0); result:0.625 because both sides are double result is double
Console.WriteLine (5.0/8); result:0.625 because there is a number that is double, the result is double
Console.WriteLine (5f/8f); result:0.625 because all float is the result of float
Console.WriteLine (5d/8d); result:0.625 because both sides are double result is double
Console.WriteLine (5/8f); result:0.625 because there is a number that is a decimal type, the result is a decimal type
------------------------------------------------
The 5th section of string declarations is primarily an escape character issue
Common data types:
String (String) A string of characters that is enclosed in double quotation marks in English.
': Char with length 0
"": A string of length 0, with a string, and an empty string equal to string. Empty
String. Empty: null string
Null: Not equal to empty string
"0":
\ (backslash): escapes the following character with a special meaning \ nthe newline character \ \ \ A slash \ "\" a pair of double quotation marks
Add "@" to the string: Tell the compiler that there are no escape characters, (@ only works for \, when quotes are quoted, do not use @)
------------------------------------------------
6th section char, bool, and string and numeric type conversions
Char: Represents a character (with and only one character), the single quotation mark ' in English, only 1 in length.
Can be an escape character: Char c= ' \ n '; ‘\\‘ ‘\"‘ ‘\‘‘
Bool:true false for both states (two values)
Int:
The maximum value int. MaxValue;
Minimum value: Int. MinValue, other numeric types can also get a range of values through MaxValue, MinValue.
Convert other data types to String:convert
A.tostring ()//null Throw exception
Recommendation: Convert.ToString ()//null not throw exception
An explicit type conversion is limited to integers and decimal types. Cast (type has intersection)
Convert String to Int/long:
Convert.ToInt32 ("123"), Convert.toint64 ("456")
------------------------------------------------
7th section What is the difference between the CTS (uppercase string and lowercase string)? )
1. Not only can C # be compiled into. Net IL, but it also supports languages such as Basic, Python, Ruby, and even Java. Data type definitions are not the same in different languages,
. The common data types (Cts,common type System) are defined in net: String, Int16, Int32, Int64, Boolean, Double. By going to the definition you can see that these are classes (structs).
2. The C # Language specification defines primitive types such as String, int, long, bool, double, and the compiler translates these types into classes in the CTS. Anti-compilation See IL can see.
string int long bool double:C # system keyword
String Int32 Int64 Boolean Double:the class defined in the CTS
------------------------------------------------
Section 8th enumeration Types
1, there are some data is open range, such as int, float, String. Some data optional value is a limited range of values, such as constellation, month name, direction, if you use 1/2/3/4 to represent the cardinal, that in case set up 8 what to do?
2. Enumeration is a special type of syntax that defines the range of values to be determined:
Enum Dir
{
East,west,north,south;
}
3. Use Dir d = dir.east;
The declaration of EUNM and the class is peer. (Special Class)
------------------------------------------------
Section 9th Basic operators (remainder, self-increment, string)
1, the operator is plus (+) minus (-) multiply (*) in addition (*) and other symbols, English keyboard input x÷ is very troublesome, so use */expression multiplication.
2, the remainder operator "%": 5%4 is 1, 5%5 is 0, 22%7 is 1.
3, self-increment: + + is a variable to self-increment operation
int x=3;
x + +;
Self-reduction:--
4, + can also be used to splice strings: "abc" + "CDE"
5, + can also be spliced with other types: "Hello" +5
6, "Hello" +5+5, "Hello" + (5+5), 5+5+ "Hello" difference.
7, the expression from the left-to-right scanning operation, once a string is encountered, the subsequent operation becomes a string.
8, question: 3+5+ "Hello" +5+3 the result is what. Answer please pull down 8hello53
The addition of strings and any other data is a concatenation of strings (other data is converted to strings).
"Hello" +5+5//hello55
5+5+ "Hello"//10hello operation from left to right
"Hello" + (5+5)//Hello10 parentheses Change the priority of operations
------------------------------------------------
Section 10th assignment Operators and implementing the addition calculator
1, int i=5;
It should be read as "declare int type variable i, and assign 5 to I".
=: The assignment operator is not meant to be equal to, only one variable on the left
+ =: i=i+3 i+=3
-=: i=i-3 i-=3
*=: I=i*3 i*=3
/=: I=i/3 i/=3
------------------------------------------------
Copy of the 11th section variable transfer and exchange values of two variables
Replication delivery. (let variable 2 point to the value of the memory that the variable 1 "current" points to)
1. Exchange values for two variables:
123456 |
int i=10; int j=20; int temp = i; //temp:10;i=10;j=20; i=j; //temp:10;i=20;j=20; j=temp; //temp:10;i=20;j=10; Console.WriteLine( "i=" +i+ ";j=" +j); |
2. Another approach that does not require intermediate variables
int
i=3;
int
j=5; // j=5
i=i+j; // i=8
j=i-j; // j=3
i=i-j; // i=5
3, Variant without intermediate variable Exchange two value method:
int i=100;
int j=4;
I=i-j;
J=i+j;
I=j-i;
------------------------------------------------
Section 12th relational operators and operator precedence
Comparison operators: (relational operators) are used to determine the authenticity of a value, and the result is a bool type.
= = equals (equal)
! = does not equal (unequal)
> Greater than
>= greater than or equal to
< less than
<= less than or equal to
Operator Precedence:
The program is not only for the compiler to see, but more importantly for programmers to see.
"()" The greatest parentheses solve all the troubles.
The readability of the program is important, do not write code that others cannot understand.
------------------------------------------------
13th-Assignment expressions also have values
1. What is the execution result of the following program?
int a=3;
int b=4;
Console.WriteLine (A==B); False
Console.WriteLine (A=B); 4
2, the assignment itself is also a value, this value is the value left after the assignment.
int i= (a=b) +3;
Console.WriteLine (i); 7
What about this b1? BOOL B1 = (A=B); Compile error: wrong type, cannot assign value
What about this B3?
BOOL B1 = false;
bool B2 = true;
BOOL B3= (B1=B2); True
------------------------------------------------
The difference between self-increment and post-increment before 14th quarter
1.
int i=10;
int i1=i++;
Console.WriteLine (I1); 10
Console.WriteLine (i); 11
int j=10;
int j1=++j;
Console.WriteLine (J1); 11
Console.WriteLine (j); 11
2,i++ is the assignment and then self-increment, ++i is the first self-increment and then assign value.
i--and-I are the same, experimenting on their own.
when there is no assignment, i++ (the value of the expression is I) and ++i (the value of the expression is i+1), no difference (write program avoids ++i)
This question is often tested in interviews!
------------------------------------------------
15th Section logical operator (with or not)
That is used to operate on values of the Boolean type.
Binary operators: +-*/%
Unary operator:!
&&: (and) a two-dollar operator with true results on both sides is true. "I am Chinese" "I am a man" as long as there is a false, then "I am Chinese and I am a man" is false.
|| : (or) a two-dollar operator, as long as one is true and the result is true. "I am Chinese" "I am a man" as long as there is a true, then "I am a Chinese or I am a man" the judgment is true.
! : (non) unary operator, reverse , true false, false change true.
------------------------------------------------
16th. Short-circuit of logical operators
Use of && | | , pay attention to the problem of contentment, the front conditions are not established, do not calculate the value of the following conditions, directly give a judgment result.
You can use & and | To troubleshoot short-circuit problems (rarely used, as long as you know the line).
Place of Use:
int i=8;
int j=0;
BOOL B= (j!=0) && (i/j==2);
Console.WriteLine (b);
Can simplify operations (to avoid the next condition in case of error)
if (Str!null && str. LENGTH>5)
------------------------------------------------
Section 17th ternary operators
Conditional expression? Expression 1: Expression 1
If the condition expression is true, the value of the expression is "expression 1", otherwise "expression 2"
------------------------------------------------
18th Section if introduction
if (comparison expression) {
One or more lines of code
}
Executes the code within {} When the comparison expression is true.
------------------------------------------------
The 19th section if easy to mistake point
1, if ("must be a conditional expression"), cannot be used if (i=10), the conditional expression in C # must be of type bool.
2, even if there is only one line of code, do not omit the curly braces {} (to form a good programming habit) (not easy to read, increase the probability of error)
3. Note that the if () cannot be followed by a ";" The closing number indicates the end of the letter.
{} represents a scope called a block of code.
if (i==8); Equivalent if (i==8) {}
------------------------------------------------
20th Section ELSE clause
The IF statement can also have else:
if (comparison expression) {
code block to execute when expression is true
}
Else
{
code block to execute when expression is False
}
Good habits: ( do not exclude the general impossibility of the situation )
When judging the length of the string, write:
if (name. Length<=0)
{
MessageBox.Show ("Please enter name");
}else
{
MessageBox.Show (name+ ", Hello! ");
}
------------------------------------------------
Section 21st ElseIf and practice
if (conditional expression 1) {
code block to execute when expression 1 is true
}
else if (conditional expression 2)
{
Expression 1 is false, expression 2 is true when the code block is executed
}
else if (conditional expression 3)
{
Expression 1 is false && expression 2 is false, expression 3 is true when code block is executed
}
else//can be used without the last else, suggested plus, more rigorous
{
Executes when the expression is false
}
------------------------------------------------
22nd. If case: City-Province linkage selection
Points:
1. Load save data to Province list (list fill)
2. Select the province to load the corresponding city list into the city list
------------------------------------------------
23rd If case: Body mass Index Calculator
1. Data type Conversion
2,(): brackets are omnipotent
3, the condition interval does not leave the gap
------------------------------------------------
The selection between the ternary operator and the IF in the 24th section
Assignment: The user enters the month and date of birth, calculates the user's constellation, and then displays a description of the constellation.
It is the hard truth to do.
1, the ternary operator can be implemented, the basic if can be implemented, but if the implementation of the ternary operator may not be able to achieve.
2, simple judgment and then based on the bool result of a simple assignment to use the ternary operator.
------------------------------------------------
25th Section switchcase
Defined:
switch (expression)
{
Case takes value 1:
Execute statement A
Break
Case takes value 2:
Execute Statement b
Break
Case takes value 3:
Execute Statement C
Break
........
Default
are not satisfied with the executed statement
Break
}
1. The expression can be byte, Short,int, char,string, enum type, (Long).
2, breakmeans the end of switch, can not be lost . Default (Can not write, but strongly recommended to write, code rigor ) is equivalent to the if else, and when all case does not match, execute default.
3, If there is no code in the case, if you do not write break, it will run through the next code.
Problems merging multiple switch conditions: When the code for multiple case conditions is the same, you can merge, and the last one adds a break;
The switch can be used to replace if when the discrete value is judged. Switch can do if all can do, the contrary is not necessarily.
such as Peng Network: http://www.rupeng.com
such as Peng Network. NET Foundation 1 Chapter II: C # language Fundamentals 1