"Variable" is only the storage location of data. You can store data in it or use it as a part of the C # expression. The meaning of the data stored in the variable is controlled by type.

C # is a strong type (???) Language. In this way, all operations on a variable are performed on the type of the variable. To ensure the validity and consistency of the data stored in variables, there are rules for operations on different types of variables.

C # The simple types of language include Boolean and three numeric types: integer, floating point, and decimal.

1. Listing 1-1 shows a Boolean value: Boolean. CS |

Using system; Class booleans { Public static void main (){ Bool content = true; Bool nocontent = false; Console. writeline ("It is {0} That C # station provides C # programming language content.", content ); Console. writeline ("the statement above is not {0}.", nocontent ); } } |

1. In listing 1-1, a Boolean value is output to the console as part of a sentence. The value of the "bool" type is either true or false. The program running result is as follows:

> It is true that C # station provides C # programming language content. > The statement above is not false. |

2. The following table shows the values of various integer types, including the size of the byte and the number range that can be expressed.

Type |
Bit |
Range |

Sbyte |
8 |
-From 128 to 127 |

Byte |
8 |
0 to 255 |

Short |
16 |
-From 32768 to 32767 |

Ushort |
16 |
0 to 65535 |

Int |
32 |
-From 2147483648 to 2147483647 |

Uint |
32 |
0 to 4294967295 |

Long |
64 |
-From 9223372036854775808 to 9223372036854775807 |

Ulong |
64 |
0 to 18446744073709551615 |

Char |
16 |
0 to 65535 |

In addition to the character type, the preceding types are suitable for integer calculation. The character type represents a Unicode character. As you can see in the preceding table, you can select the appropriate type.

3. The following table shows the single-precision, double-precision, and decimal data, the bytes they occupy, the precision, and the range of data that can be expressed.

Type |
Bit |
Precision |
Range |

Float |
32 |
7. digits |
1.5x10-45 to 3.4x1038 |

Double |
64 |
15-16 digits |
5.0x10-324 to 1.7x10308 |

Decimal |
128 |
28-29 decimal places |
1.0x10-28 to 7.9x1028 |

When you need to perform a score-related operation, you need to use the real type. However, for financial and financial data calculation, the decimal type may be your best choice.

4. The result can be obtained after the expression is calculated. These expressions place variables and operators together in the statement. The following table lists the operators, priorities, and associativity allowed by C.

Category |
Operator |
Associativity |

Elementary |
(X) x. y f (x) A [x] x ++ X -- new typeof sizeof checked unchecked |
Left |

Single Camera |
+ -! ~ ++ X -- x (t) x |
Left |

Multiplication |
*/% |
Left |

Addition |
+- |
Left |

Shift |
<> |
Left |

Link |
<> <=> = Is |
Left |

Equal |
=! = |
Right |

Logic and |
& |
Left |

Logic exclusive or |
^ |
Left |

Logic or |
| |
Left |

Condition and |
&& |
Left |

Condition or |
| |
Left |

Condition |
? : |
Right |

Assignment |
= * =/= % = + =-= <= >>=& = ^ = | = |
Right |

The left combination means that the operator is operated from left to right. Right combination means that all operations are performed from right to left, such as the value assignment operator. The result is placed into the variable on the left after the calculation on the right.

2. Listing 1-2. Single Object OPERATOR: unary. CS |

Using system; Class unary { Public static void main (){ Int unary = 0; Int preincrement; Int predecrement; Int postincrement; Int postdecrement; Int positive; Int negative; Sbyte bitnot; Bool lognot; Preincrement = ++ unary; Console. writeline ("pre-increment: {0}", preincrement ); Predecrement = -- unary; Console. writeline ("pre-Decrement: {0}", predecrement ); Postdecrement = unary --; Console. writeline ("post-Decrement: {0}", postdecrement ); Postincrement = unary ++; Console. writeline ("post-increment: {0}", postincrement ); Console. writeline ("final value of unary: {0}", unary ); Positive =-postincrement; Console. writeline ("positive: {0}", positive ); Negative = + postincrement; Console. writeline ("negative: {0}", negative ); Bitnot = 0; Bitnot = (sbyte )(~ Bitnot ); Console. writeline ("bitwise NOT: {0}", bitnot ); Lognot = false; Lognot =! Lognot; Console. writeline ("logical not: {0}", lognot ); } } |

1. When calculating an expression, when performing an operation by the plus and minus one operators after the operation, the value is first returned, and then an increment or decrement operation is performed. When you use the plus sign and minus sign operators, you must first add or subtract one, and then return the result value.

2. In listing 1-2, the variable unary is initialized to 0. When ++ X is performed, the value of "unary" is added with 1, and then the value 1 is assigned to the "preincrement" variable. When performing the -- X operation, first reduce the value of "unary" to 0, and then assign the value 0 to the "predecrement" variable.

3. When performing the X-operation, first assign the value 0 of "unary" to the "postdecrement" variable, and then reduce "unary" to-1. When performing the X ++ operation, first assign the value-1 of "unary" to the "postincrement" variable, and then add 1 to "unary, the current value of the "unary" variable is 0.

4. the initial value of the variable "bitnot" is 0. bitwise inversion is performed. In this example, the number 0 is represented as binary "00000000" and the bitwise inversion is changed to-1, the binary value is 11111111 ".

5. Understand the expression "(sbyte )(~ Bitnot) ", any operation on data of the sbyte, byte, short or ushort type will return an integer. To assign a value to the bitnot variable, we must use the cast (type) operator (forced type conversion). type indicates the type you want to convert to (sbyte in this example ). The cast operator must be cautious when converting data of a large range to a small range of data, because there is a risk of data loss. Generally, it is no problem to assign small data types to large variables, because a large range of data types have enough space to store small data types. Note This risk also exists when performing cast operations between the signed and unsigned types. Many preliminary programming tutorials provide a good explanation of the bit representation of variables and also introduce the danger of directly performing cast operations.

Non-logical (!) Operators can process boolean values. In this example, the variable "lognot" is changed from false to true.

The output result of the above program is as follows:

> Pre-increment: 1 > Pre-Decrement 0 > Post-Decrement: 0 > Post-increment-1 > Final value of unary: 0 > Positive: 1 > Netative:-1 > Bitwise NOT:-1 > Logical not: True |

3. Listing 1-3. binary operator binary. CS |

Using system; Class binary { Public static void main (){ Int X, Y, result; Float floatresult; X = 7; Y = 5; Result = x + y; Console. writeline ("X + Y: {0}", result ); Result = x-y; Console. writeline ("x-y: {0}", result ); Result = x * Y; Console. writeline ("x * Y: {0}", result ); Result = x/y; Console. writeline ("x/y: {0}", result ); Floatresult = (float) x/(float) y; Console. writeline ("x/y: {0}", floatresult ); Result = x % Y; Console. writeline ("X % Y: {0}", result ); Result + = X; Console. writeline ("result + = x: {0}", result ); } } |

Listing 1-3 demonstrates several examples of binary operators. The results of addition (+), subtraction (-), multiplication (*), and Division (/) are the results of the four arithmetic operations we normally perform.

Because the "floatresult" variable is a floating point operation type, the integer variables "x" and "y" are forcibly converted to the floating point type to calculate the floatresult.

Here is an operator used to evaluate the remainder. two operands are divided and return the remainder.

The last statement provides another form of value assignment. The (+ =) operator is used here. no matter when you use the (+ =) operator, this binary operator should be computed on both sides of the operator and then assigned the value to the parameter on the left. This statement is equivalent to "result = Result + x" and returns the same value.

In the previous course, one type that you see is "string" (string. The "string" type is a Unicode character that is enclosed in quotation marks. For example, "This is a string ."

Another data type is array. Arrays can be seen as a set of elements of the same type. When declaring an array, you must specify the type name, array name, dimension, and array size.

4. Listing 1-4. Array Operations: array. CS |

Using system; Class Array { Public static void main (){ Int [] myints = {5, 10, 15 }; Bool [] [] mybools = new bool [2] []; Mybools [0] = new bool [2]; Mybools [1] = new bool [1]; Double [,] mydoubles = new double [2, 2]; String [] mystrings = new string [3]; Console. writeline ("myints [0]: {0}, myints [1]: {1}, myints [2]: {2}", myints [0], myints [1], myints [2]); Mybools [0] [0] = true; Mybools [0] [1] = false; Mybools [1] [0] = true;Console. writeline ("mybools [0] [0]: {0}, mybools [1] [0]: {1}", mybools [0] [0], mybools [1] [0]); Mydoubles [0, 0] = 3.147; Mydoubles [0, 1] = 7.157; Mydoubles [1, 1] = 2.117; Mydoubles [1, 0] = 56.00138917; Console. writeline ("mydoubles [0, 0]: {0}, mydoubles [1, 0]: {1}", mydoubles [0, 0], mydoubles [1, 0]); Mystrings [0] = "Joe "; Mystrings [1] = "Matt "; Mystrings [2] = "Robert "; Console. writeline ("mystrings [0]: {0}, mystrings [1]: {1}, mystrings [2]: {2}", mystrings [0], mystrings [1], mystrings [2]); } } |

Listing 1-4 demonstrates different implementation methods of arrays. The first example is the "myints" array, which is initialized while declaring.

Next is a two-dimensional array, which can be understood as an array. We need to use the "new" operator to instantiate the size of the initial array, and then use the new operator for each subarray.

The third example is a two-dimensional array. Arrays can be multidimensional, and each dimension can be separated by commas. They must also be instantiated using the "new" operator.

Finally, a one-dimensional string array is defined.

In each case, access to data elements can be performed by referencing the position (subscript) of the element. The array size can be any integer value. Its subscript starts from 0.