C # expressions
expressions, which combine variables, literals, and operators to create expressions, are the basic components of a calculation.
Literal: Is the number, string, or value typed in the source code that represents the specified type. There are several commonly used: integers, real numbers, characters, strings .
Full digital polygon "can also use hexadecimal table":
-
- 123//Plastic
- 123L//Long Plastic
- 123U//unsigned shaping
- 123UL//unsigned long shaping
Real Digital Polygon Amount:
-
- 1.5F//float Type
- 1.5//double Type
- 1.5M//decimal Type
Character literal:
-
- ' A '//char type
- ' \ n '//simple escape sequence: Backslash + single character
- ' \x0061 '//16 binary escape sequence
- ' \u005a '//unicode escape sequence
string literal:
- "Hi There";
- "Val\t5,val\t10";
- @ "C:\Program Files\Microsoft";
Note: The string is prefixed with @, and all content in double quotes, including content that is normally considered an escape sequence, is strictly printed as listed in the string.
operator, broadly divided into 3 classes.
- Unary operator, which handles one operand
- Binary operator, handling two operands "most"
- Ternary operator, handling three operands
numeric operators
Operator |
Category |
An example expression |
Results |
+ |
Two Yuan |
Var1=var2+var3 |
The value of var1 is the sum of var2 and VAR3 |
- |
Two Yuan |
Var1=var2-var3 |
The value of var1 is the difference between var2 and VAR3. |
* |
Two Yuan |
Var1=var2*var3 |
The value of VAR1 is the product of VAR2 and VAR3 |
/ |
Two Yuan |
Var1=var2/var3 |
The value of VAR1 is var2 divided by the value of VAR3. |
% |
Two Yuan |
Var1=var2%var3 |
The value of var1 is the remainder of var2 divided by var3 |
+ |
One dollar |
Var1=+var2 |
The value of var1 equals the value of VAR2 |
- |
One dollar |
Var1=-var2 |
The value of var1 equals the value of var2 multiplied by-1 |
String operators
Operator |
Type |
An example expression |
Results |
+ |
Two Yuan |
Var1=var2+var3 |
The value of VAR1 is two string concatenated values stored in VAR2 and VAR3 |
Increment and decrement operators
- + + Always use operands plus 1
- --Always use operands minus 1
Operator |
Type |
An example expression |
Results |
++ |
One dollar |
VAR1=++VAR2; |
The value of var1 is var2+1,var2 increment by 1 |
-- |
One dollar |
VAR1=--VAR2; |
The value of var1 is var2-1,var2 decrement by 1 |
++ |
One dollar |
var1=var2++; |
The value of var1 is var2,var2 increment by 1 |
-- |
One dollar |
var1=var2--; |
The value of var1 is var2,var2 decrement by 1 |
Assignment operators
Operator |
Type |
An example expression |
Results |
= |
Two Yuan |
VAR1=VAR2; |
Var1 is given the value of VAR2 |
+= |
Two Yuan |
VAR1+=VAR2; |
Var1 is endowed with var1 and VAR2 and |
-= |
Two Yuan |
VAR1-=VAR2; |
Var1 was given the difference between var1 and var2. |
*= |
Two Yuan |
VAR1*=VAR2; |
VAR1 is given the product of VAR1 and VAR2 |
/= |
Two Yuan |
VAR1/=VAR2; |
Var1 is given the result of dividing var1 and var2 |
%= |
Two Yuan |
VAR1%=VAR2; |
Var1 is given the remainder obtained by dividing the var1 from the VAR2 |
Operator Precedence
Priority level |
Operator |
Excellent First Level By High To Low |
++,--(used as a prefix); +,-(unary) |
*,/,% |
+,- |
=,*=,/=,%=,+=,-= |
++,--(used as a suffix) |
Note: parentheses can be used to override the order of precedence.
C # Getting Started note 3 expressions and operators