2. C # basic organization (operators, data types, and conversions ),
· Operators
Mathematical operators: +-*/%
Comparison OPERATOR: <>=>=! = Return bool Value
Logical operators: &, | or, the Operation Sequence of the two is not sequential.
Non-logical"! ": Applies only to the followed code, mathematical operators (in case of parentheses) <priority <comparison operator
Operation Sequence: "(*/%)"> "(+-)", logical operator, and value assignment operator "="
Auto increment "++" auto increment "--": Add, subtract, multiply, and divide a value before writing a variable. add, subtract, multiply, and divide a value after writing a variable. For example:
int a = 0; int b = 0; int c = ++a + b; int d = a++ + b; Console.WriteLine(c +","+ d+","+a);
The output results of c, d, and a are, 2, respectively.
Conditional operators (comparison expressions )? (Satisfying expression: Statement 1) :( not satisfying expression: Statement 2)
Other value assignment operators: + =-=/= * = % =
· Data Type Value Type
Int, long, double, float, decimal
1. sbyte-Signed byte-Unsigned (positive number) 255
2. int long short -- signed uint ulong ushort -- unsigned
3. enum enumerative struct
Reference Type
1. class delegates dynamic interface object string
2. All types of the object base class parent class (collectively referred to)
Constant (const)
Const converts a variable to a constant. Method: const int = 1
· Type conversion
1. Convert the value type to the value type: forced conversion. Example:
int i = 4; int s = 5;double jieguo = (double)i/s; double i1 = 4.5; int s1 = 5;double jieguo1 = i1 / (double)s1;
2. Convert the value type to the reference type: explicit conversion. Example:
// Convert to the string ToStringstring j = I. ToString (); Console. WriteLine (j); // j is now in the string format
3. Convert the reference type to the value type: implicit conversion (it must be within the range of the value type, for example, "1.23" cannot be converted to the int Integer type ). Example:
String st = "1.35"; double dd = double. parse (st); double ds = Convert. toDouble (st); // another method int ss = int. parse (st); // Wrong! Cannot run int ii = (int) double. parse (st); // when converting to an integer, you must first convert it to the double type. The default floor value is Console. writeLine (dd); // The result is 1.35lele.writeline (ii); // The result is 1.
· Exercise:
1. Enter the bottom radius and height on the console to obtain the cylindrical surface area.
Console. writeLine ("Enter the bottom edge radius and Height:"); double di = double. parse (Console. readLine (); double h = double. parse (Console. readLine (); double dimianji = 3.14 * r; double cemianji = 2*3.14 * r * h; double biaomianji = dimianji * 2 + cemianji; Console. writeLine ("the cylindrical surface area is: {0}", biaomianji );View answer
2. Enter a month from the console, if the month is 1 ~ In June, "This is the first half of the year" was output; otherwise, "this is the second half of the year"
Console. writeLine ("enter a month:"); int mon = int. parse (Console. readLine (); bool pan = mon <= 6; if (pan = true) Console. writeLine ("This is the first half of the year"); else Console. writeLine ("this is the second half of the year ");View answer