|
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 as 7 lbs.
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.
Summary
So far, you have learned about C # variables, simple data types, arrays and strings. We also learned how to use the C # Operator to construct an expression. Do you know what you actually learned?