Expressions and Operator expressions
Literal integer literal number
Real Digital Polygon Amount
Character literal
string literal evaluation order Precedence
Associative simple arithmetic operators
Remainder operator
Relational comparison operators and equality comparison operators
Increment operator and decrement operator
Conditional logical operators
logical operators
Shift Operators
Assignment operator expressions and operator expressions
This chapter defines an expression and describes the operators that are provided by C #.
An operator is a symbol that represents an operation that returns a single result. The operand (operand) refers to the data element that is entered as an operator. An operator will:
- Use operand as input
- Perform an action
- Returns a value based on this operation
An expression is a string of operators and operands. The structures that can be used as operands are:
- Literal quantity
- Constant
- Variable
- Method invocation
- Element accessors, such as array accessors and indexers
- Other expressions
Example: The following expression, with 3 operators and 4 operands
Literal quantity
Literal (literal) is a number or string typed in the source code that represents an explicit, fixed value of a specified type.
Example: Literal
classprogram{Static voidMain () {Console.WriteLine ("{0}",1024x768);//integer literalConsole.WriteLine ("{0}",3.1416F);//floating-point literalsConsole.WriteLine ("{0}",true);//boolean literal }}
For reference-type variables, the literal null means that the variable is not set to the in-memory data.
Integer Digital surface Amount
Example: Different integer types
236 // Integral type 236L // Long Integer type 236U // unsigned integral type 236UL // unsigned long integer type
The entire digital surface can also be written in 16 hex form
Real Digital Polygon Amount
The actual number of surface quantities is composed as follows:
- Decimal digits
- Optional decimal point
- Optional Index section
- Optional suffix
Example: A different format for real number literals
float f1=236F; double d1=236.712; Double d2=. 351 ; double d3=6.338e-26;
The real literal literals of no suffix are the double type by default.
Character literal
The character literal can be any one of the following:
- Single character
- Simple escape sequence: Backslash + single character
- Hex escape sequence: backslash + uppercase or lowercase x+4 hexadecimal number
- Unicode escape sequence: backslash + uppercase or lowercase u+4 hexadecimal number
Example: different formats for character literals
Char c1='d'; Char c2='\ n'; Char c3='\x0061'; Char c4='\u005a';
Some special characters and their codes are shown in
string literal
Two types of string literals:
- Regular string literals
- Verbatim string literals
Regular string literals contain:
- Character
- Simple escape sequences
- Hexadecimal and Unicode escape sequences
string st1="Hi there! " ; string st2="val\t5,val\t10"; string st3="add\x000asome\u0007interest";
Verbatim strings are prefixed with @, which has the following characteristics:
- A verbatim string differs from a regular string in that an escaped string is not evaluated. Everything in the middle of a double quotation mark, including what is usually considered an escape sequence, is printed exactly as it is listed in the string
- The only exception to a verbatim string is the adjacent double-quote group, which is interpreted as a single double-quote character
stringrst1="Hi there!";stringvst1=@"Hi there!";stringRst2="It started,\ "Four score and seven...\"";stringVst2=@"It started, "Four score and seven ..."";stringrst3="Value 1 \ 5,val2 \ t";stringvst3=@"Value 1 \ 5,val2 \ t";stringrst4="C:\\Program Files\\microsoft\\";stringvst4=@"C:\Program files\microsoft\";stringrst5="Print \x000a multiple \u000a Lines";stringvst5=@"Print multiple Lines";
The compiler lets the same string literal share the same memory location in the heap to conserve memory
Order of Evaluation
An expression can consist of many nested sub-expressions. The evaluation order of sub-expressions can make the final value of an expression different.
Priority level
Just as primary multiplication is added and reduced, the operators in C # also have precedence.
Binding nature
Operators in an expression have different precedence, from high to low. But what if the operator has the same precedence?
When successive operators have the same precedence, the order of evaluation is determined by the operational binding.
- Left-associative operator from left to right
- Right associative operator from right to left
- In addition to the assignment operator, the other two-tuple operators are left-associative
- Assignment operators and conditional operators are right-associative
Simple arithmetic operators
Remainder operator
The remainder operator (%) uses the second operand, in addition to the first operand, and returns the remaining number.
The remainder operator is a two-element left associative operator.
- 0%3=0, because 0 is 3 more than 0 0.
- 2%3=2, because 2 in addition to 3 of 0 more than 2
- 4%3=1, because 4 is 3 more than 1 1.
Relational comparison operators and equality comparison operators
They are all two-dollar left associative operators that compare their operands and return a bool value.
Unlike C and C + +, numbers do not have boolean meaning in C #
int x=5; if (x) // wrong, x is an int type, not a Boolean type if (x==5) // Yes, returns True
Compare operations and equality operations
For most reference types, comparing their equality, only their references are compared.
- If reference is equal, that is, they point to the same object in memory, equality is true, otherwise false, even if two detached objects in memory are exactly equal in all other respects.
- This is called shallow comparison
Illustrates the comparison of reference types
- To the left of the graph, A and B refer to the same, returning true
- On the right side of the graph, references are different, all even if the content is the same, return false
The string type is also a reference type, but it is compared in different ways. Compares the equality of strings, compares their lengths and their contents (case-sensitive)
- Returns true if two string lengths and content are equal, even if they occupy different memory regions
- This is known as deep comparison (comparison)
The delegate that will be introduced in chapter 15th is also a reference type, and a deep comparison is also used. When comparing the equality of delegates, let the ancient country two delegates be null, or the invocation list of both have the same number of members, and the invocation list wants to match, returns true.
Compares a numeric expression to compare a type and a value. Compares the actual value of the operand when the enum type is compared. The enumeration is elaborated in the 13th chapter.
Increment operator and decrement operator
Affects only the value returned to the expression, regardless of whether the operator is pre-or post-placed. After the statement is executed, the value of the variable that is ultimately stored in the operand is the same
Conditional logical operators
The conditional logical operator uses the short circuit mode operation, meaning that if the result is determined after the EXPR1 is evaluated, it skips the evaluation of the EXPR2.
Example: Short Circuit examples
BOOL bval;bval= (1= =2) && (2= =2); // false on the left, then &&, the result must be false, so skip to the right of the Operation bval= (1= =1) | | (1= =2); // left True, followed by | | operation, the result must be true, so the operation on the right is skipped
Because of short-circuit characteristics, do not place expressions with side-effects (such as changing a value) in EXP2 because they may not be evaluated.
bool BVal; int ival=; BVal= (1= =2) && (9==ival++); // results: bval=false,ival=10; ↑ ↑ False does not calculate
logical operators
Shift Operators
Example: shift operator Examples
- Each bit of operand 14 moves to the left 3 positions
- Right end vacated position with 0 supplement
- Result is 112
int a,b,x=; a=x<<3; b=x>>3; Console.WriteLine ("{0}<<3={1}", X,a); Console.WriteLine ("{0}>>3={1}", x,b);
Assignment operators
The assignment operator is a two-dollar right-associative operator
Compound Assignment
The compound assignment operator allows a shorthand method in which, in some cases, the left-hand variable is prevented from appearing repeatedly on the right.
Compound assignment is not only shorter, but also easy to understand.
x=x+ (Y-z); x+=y-z;
Eighth chapter expressions and operators