C# study notes—Exception catching and variable operators
Exception catching
Use exception capture to capture code blocks where exceptions occur to prevent the program from getting stuck due to exceptions being thrown.
try{}catch{}finally{} structure
//Exception catching
try
{
string str=Console.ReadLine();
int i=int.Parse(str);
Console.WriteLine("Convert the input string value to an int value"+i);
}catch
{
Console.WriteLine("Please enter a legal number");
}finally
{
//Will be executed regardless of normal execution or exception capture.
Console.WriteLine("Execution completed");
}
operator
Arithmetic operator
Arithmetic operators are operators for English numerical type variables, and the result of the operation is still a numerical value.
Assignment operator: =
Note: The assignment operator understands to assign the value on the right to the variable on the left.
Arithmetic operators:
//Arithmetic operator
//add+
int i1=5;
int sum=1+2+3+i1*2;
//reduce -
int j1=6;
int sub=15-j1-2;
//take *
int c=5;
int c1=5*c;
//remove /
int d=28;
int d1=d/4;
//Modulo (remainder) %
int e=12;
e=e%5;
Console.WriteLine(e);
Priority of arithmetic operators
Remainder from multiplication and division > Addition and subtraction
int a = 1 + 2 * 3 / 2 + 1 + 2 * 3; //=11
Console.WriteLine(a);
a = 1 + 4 % 2 * 3 / 2 + 1; //2
Console.WriteLine(a);
//Brackets can change the priority. The content inside the brackets is calculated first.
a = 1 + 4 % (2 * 3 / 2) + 1; //3
Console.WriteLine(a);
//Multiple sets of brackets. Calculate the innermost bracket first and then work outwards.
a = 1 + (4 % (2 * (3 / 2))) + 1; //2
Console.WriteLine(a);
Composite operators
+= -= *= /= %= is equivalent to reassigning the result of an arithmetic operation to itself
//Conformity operator
int i3 = 1;
i3 = i3 + 2;
Console.WriteLine(i3);
i3 = 1;
i3 += 2;//i3 = i3 + 2;
Console.WriteLine(i3);
i3 = 2;
i3 += 2;//4
i3 -= 2;//2
i3 /= 2;//1
i3 *= 2;//2
i3 %= 2;//0
Console.WriteLine(i3);
int i4 = 10;
// i4 += 4
i4 += 20 * 2 / 10;
Console.WriteLine(i4);
//Note: Composite operators can only perform one operation and cannot mix operations.
//i4 */-= 2;
Increment/decrement operators
Pay attention to understand whether the operator is prefixed or postfixed, and the difference between using it first and then incrementing/decrementing it, or using it first and then incrementing/decrementing it.
It can be understood as Dengeki Boy fighting monsters. Xiaoguang first transforms into Dengeki Boy and fights monsters, or he transforms after defeating monsters.
//Increment operator
//Increase operator to make yourself +1
a2 = 1;
a2++;//Use first and then add
Console.WriteLine(a2);
++a2;//Add first and then use
Console.WriteLine(a2);
a2 = 1;
Console.WriteLine(a2++);//1
//2
Console.WriteLine(++a2);//3
//Self-decrement operator makes itself -1
a2 = 1;
a2--;//Use first and then subtract
--a2;//Decrease first and then use
a2 = 1;
Console.WriteLine(a2--);//1
//0
Console.WriteLine(--a2);//-1
//think? this
int a = 10, b = 20;
// 11 + 20
int number1 = ++a + b;
Console.WriteLine(number1);//31
a = 10;
b = 20;
//10 + 20
int number2 = a + b++;
Console.WriteLine(number2);//30
a = 10;
b = 20;
//10 + 21 + 11
int number3 = a++ + ++b + a++;
Console.WriteLine(number3);//42
Console.WriteLine(a);//12
String concatenation
-
Use +splicing
string str = "123"; //Use + sign to concatenate strings str = str + "456"; Console.WriteLine(str); str = str + 1; Console.WriteLine(str); //Use += str = "123"; str += "1" + 4 + true; Console.WriteLine(str); //Note: As long as a string is encountered, it is converted to a string string str = "Chanzhi"; str += 1 + 2 + 3 + 4; Console.WriteLine(str);//Changzhi 10 str = ""; str += "" + 1 + 2 + 3 + 4;//It becomes a string at the beginning Console.WriteLine(str);//1234 str = ""; str += 1 + 2 + "" + (3 + 4); // Calculate the parentheses first and calculate from beginning to end Console.WriteLine(str);//37 str = "123"; str = str + (1 + 2 + 3); Console.WriteLine(str);//1236
-
Use placeholder replacement to splice (placeholders start from 0 and are enclosed in {})
string str2 = string.Format("I am {0}, I am {1} years old, and my hobbies are: {2}", "Changzhi", 21, "I Love blogging!!"); Console.WriteLine(str2); str2 = string.Format("asdf{0},{1},sdfasdf{2}", 1, true, false); Console.WriteLine(str2);
Conditional operator
Conditional operators are all binary operators, and the return result is of bool type. The operation results are used to make judgments in certain situations.
Conditional operators have lower precedence than arithmetic operators
//Conditional operator
int a = 5;
int b = 10;
//more than the
bool result = a > b;
Console.WriteLine(result);
//Less than
result = a = b;
Console.WriteLine(result);
//less than or equal to
result = a 10 || 3 1 || b 5;
Console.WriteLine(result);
// ? && ?
// ? || ?
// ? Can be a hard-coded bool variable or bool value
// It can also be related to conditional operators
//----------logical not!
//symbol !
//Rule to negate a bool value, true to false, false to true
result = !true;
Console.WriteLine(result);
result = !false;
Console.WriteLine(result);
result = !!true;
Console.WriteLine(result);
//logical negation has higher priority
result = !(3 > 2);
Console.WriteLine(result);
a = 5;
result = !(a > 5);
Console.WriteLine(result);
//Priority issues when using mixed logical operators
// Rules !(logical NOT) has the highest priority &&(logical AND) has higher priority than ||(logical OR)
// The priority of logical operators is lower than that of arithmetic operators and conditional operators (except logical NOT)
bool gameOver = false;
int hp = 100;
bool isDead = false;
bool isMustOver = true;
//false || false && true || true;
//false || false || true;
result = gameOver || hp < 0 && !isDead || isMustOver;
Console.WriteLine(result);
Short-circuit principle of logical operators (smart operators)
//Short circuit principle
//|| The judgment principle is true if it is true. Therefore, if the first condition is judged to be true, it can be directly concluded that the operation result is true, and the second one will not be checked.
//True or false of each operation condition
//Similarly, if the condition on the left of && is false, the condition on the right will not be judged, and the operation result can be directly concluded to be false.
//Logical OR is true if it is true. As long as the left side is true, the right side is not important.
int i3=1;
bool result = i3 > 0 || ++i3 >= 1;
Console.WriteLine(i3);//1
Console.WriteLine(result);
// false && i3 ++ > 1; discard and do not calculate later
//Logical AND If false, then as long as the left side is false, the right side is not important
result = i3 1;
Console.WriteLine(i3);//1
Console.WriteLine(result);
//think?
//What is the print result?
//Pay attention to the priority of the operator
bool gameOver;
bool isWin;
int health = 100;
gameOver = true;
isWin = false;
// true || false && true
Console.Write(gameOver || isWin && health > 0);
bit operator
Bitwise operations are operations based on binary encoding. First, the value is converted into a binary value, and then the bits are manipulated.
Operators: bitwise AND & bitwise or | exclusive OR ^ bitwise or | bitwise negation! Move left<>
//Bit operators
//If the bit AND& is 0, it will be 0, and if it is all 1, it will be 1
// Bitwise operations If there are 0, then 0
int a = 1; // 001
int b = 5; // 101
// 001
//& 101
// 001 = 1
int c = a & b;
Console.WriteLine(c);
//Perform bitwise operations on multiple values. When there are no parentheses, calculate from left to right.
a = 1; // 001
b = 5; // 101
c = 19; //10011
// 00001
//& 00101
// 00001
//& 10011
// 00001
int d = a & b & c;
Console.WriteLine(d);
//Bitwise OR | If there is 1, then 1, if all 0, then 0
a = 1;//001
b = 3;//011
c = a | b;
// 001
//| 011
// 011
Console.WriteLine(c);
a = 5; // 101
b = 10; // 1010
c = 20;//10100
// 00101
//| 01010
// 01111
//| 10100
// 11111 => 1 + 2 + 4 + 8 + 16 =31
Console.WriteLine(a | b | c);
//XOR^ =======
//different is 1, same is 0
// Bitwise operations: the same is 0, the difference is 1
a = 1; //001
b = 5; //101
// 001
//^101
// 100
c = a ^ b;
Console.WriteLine(c);
a = 10; // 1010
b = 11; // 1011
c = 4; // 100
// 1010
//^ 1011
// 0001
//^ 0100
// 0101 = 5
Console.WriteLine(a ^ b ^ c);
//Bit negation ~ negation
// Bitwise operation: 0 changes to 1, 1 changes to 0
a = 5;
// 0000 0000 0000 0000 0000 0000 0000 0101
// 1111 1111 1111 1111 1111 1111 1111 1010
// Knowledge of complement code
// Binary numbers in computers are stored in two's complement form
//Complement code: The complement code of a positive number is itself. The complement code of a negative number is the inverse of the absolute value plus one.
c = ~a;
Console.WriteLine(c);
//Move left, move right
// Rule: Let the binary number of a number be shifted left and right
// Shift a few bits to the left and add a few 0s to the right
a = 5; // 101
c = a <> 2;
// 1 digit 10
// 2 bits 1
Console.WriteLine(c);
//practise----
//What is the result of 99 ^ 33 and 76 | 85?
// 1100011 ^ 100001
// 1100011
//^0100001
// 1000010
Console.WriteLine(99^33);
// 1001100 | 1010101
// 1001100
//|1010101
// 1011101 => 64 + 29 = 93
Console.WriteLine(76 | 85);
Ternary operator
Condition? A: If the condition B is true, then go to A logic, otherwise go to B
//Ternary operator
int a = 5;
str = a 1 ? 123 : 234;
//The first empty position is always an expression whose result is bool type bool variable, conditional expression, logical operator expression
//Any expression can be used in the second and third spaces, as long as their result types are consistent
bool b = a > 1 ? a > 6 : !false;
======
I will update C# study notes every day. If you are interested, you can subscribe!
Click to subscribe and practice C# code without getting lost!
Make sure their result types are consistent
bool b = a > 1 ? a > 6 : !false;
======
I will update C# study notes every day. If you are interested, you can subscribe!
Click to subscribe and practice C# code without getting lost!