This section will introduce the expression, type, and variables of the C # language. This section must reach the following purposes: 1. Learn what is "variable"
2. Learn simple types of C #
3. A preliminary understanding of the C # expression
4. Learn what is string type
5. Learn how to use arrays
"Variable" is merely the storage location of the data. You can store data in it or take it as part of the C # expression. The meaning of the data stored in the variable is controlled by a type.
C # is a strong type (???) language. Thus, everything is performed on the type of variable for this variable. In order to ensure the legality and consistency of the data stored in the variable, there is a corresponding rule for different types of variables.
The simple type of the C # language contains Boolean types and three numeric types: integer, floating point and decimal.
1. Listing 1-1 Display Boolean: 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;}
Description
1. In Listing 1-1, the Boolean value is output to the console as part of the sentence. The value of the "BOOL" type is either true or false. The procedure run 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 various integer types, the one-byte size and the range of the number that can be expressed.
Type bit range sbyte 8 -128 to 127 byte 8 0 to 255 short 16 -32768 to 32767 ushort 16 0 to 65535 int 32 -2147483648 to 2147483647 uint 32 0 to 4294967295 long 64 -9223372036854775808 to 9223372036854775807 ulong 64 0 to 18446744073709551615 char 16 0 To 65535
When calculating integers, these types are suitable for the type of characters. The character type represents a Unicode character. As can be seen in the above table, you can choose the type that suits you.
3. The following table shows the single-precision type, double precision type, and decimal type data, which occupies bytes, accuracy, and scope of the number that can be represented.
Type bit accuracy range FLOAT 32 7 Digits 1.5 x 10-45 to 3.4 x 1038 Double 64 15-16 Digits 5.0 x 10-324 To 1.7 x 10308 Decimal 128 28-29 Decimal Places 1.0 x 10-28 to 7.9 x 1028
When you need to perform an operation involved in the score, you need to use the real shape, however, for the calculation of the financial finance, the decimal type may be your best choice.
4. The result can be obtained after the expression is calculated. These expressions put the variables and operators into the statement. The following table lists the operators, priority, and conjunction between C # allowed.
Classification Operator Combined Primary (x) XY f (x) a [x] x x - new typeof sizeof checked unchecked left single point -! ~ x - x (t) x left multiplication or other * /% Left plus method and other - left shift << >> Left relationship <> <=> = IS left phase ==! = Right logic and & left logical vary or ^ left logic or | Left condition and && left condition or | Condition?: Right assignment, etc. = * = / =% = = - = << = >> = & = ^ = | = right left combination means that the operator is calculated from left to right. Right combination means that all operations are made from right-to-left, such as assignment operators, and then put the result in the left variable after it is calculated.
2. Listing 1-2. Single 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}"; predecrement = --unary; console.writeline ("pre-data: {0}", predeecrement; 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);}}
Description
1. When calculating the expression, when the rear increasing and rear reduction operator is calculated, return its value, and then add or unhappy. When calculating with the preamplifting and minuscarrier operator, it is first possible to increase or decrease in calculations, and then return its result value.
2. In Listing 1-2, the variable unary is initialized to 0, when the X operation, "unary" is added 1, and the value 1 is assigned to the "preincrement" variable. When you perform --X operation, first reduce the value of "unary" to 0, and then assign the value 0 to the "PREDECREMENT" variable. 3. When performing X-operation, first assign the value of "unary" to the "postDecrement" variable, then reduce "unary" to -1. When performing X operation, first assign "unary" value -1 to the "postIncrement" variable, then add 1 "Unary", so that the "unary" variable is 0.
4. Variable "BitNot" initial value is 0, according to bit to reverse operation, in this case, 0 is represented as binary "00000000", and it becomes -1 after bitcking, its binary is expressed as "11111111".
5. Understand the expression "(~ bitNot), any calculation of the type SByte, Byte, Short or Ushort type data, the return result is an integer. To assign the value to the BitNot variable, we must use the Cast operator (forced type conversion), where Type indicates the type you want to convert (Sbyte in this example). When the CAST operator converts large-scale types of data to small-range types of data, they must be particularly cautious, as there is a risk of losing data at this time. In general, small types of data is assigned to large type variables, and there is no problem because the variables of the large-scale data type have sufficient space to store small types of data. Note that this risk is also present when the CAST operation is performed between the signed and the unsigned type. Many primary programming tutorials have made a good explanation of the variable, and also introduce the risk of directly CAST operations.
The logical non-(!) Operator can handle the Boolean variable value. In this case, the "lognot" variable changes from FALSE to True.
The output of the above procedure is as follows:
> Pre-inccess: 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 = xy; console.writeline ("xy: {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 calculation results of the addition ( ), subtraction (-), multiplication (*) and division (/) are the results of our usual four arithmetic results.
Because the "floatresult" variable is a floating point operation type, the integer variable "x" and "y" are forced to convert into floating point types to calculate the floatresult.
Here is an operator, the two operands are removed, and the remainder is returned.
The last statement gives another type of assignment, here the ( =) operator. Whenever you use the ( =) operator, then this binary operator should operate on both sides of the operator. Then assign the value to the left parameters. This statement is equivalent to "Result = Result X" and returns the same value.
In the previous course, a type of usage is more types of usage is the "String" type. The "String" type is composed of characters included in the Unicode encoded in quotation marks. For example, "this is a string."
Another data type is an array. The array can be seen as a collection of elements of the same type. When declares an array, you have to 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 [,] mydubles = 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}, mydubles [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]);}}
Description
Listing 1-4 demonstrates a variety of different implementation methods. The first example is an "Myints" array, which is initialized while the declaration.
Then it is a two-dimensional array that can be understood as an array of arrays. We need to use the "New" operator to instantiate the size of the initial array, then use the New operator for each subset.
The third example is a two-dimensional array. The array can be multi-dimensional, each dimension can be spaced apart from a comma, but also instantiate with the "New" operator.
Finally, a one-dimensional string array is defined.
In each case, the access to the data element can be performed by the location of the reference element (subscript). The magnitude of the array can be any integer value. It starts from 0.
Summary, you have learned the variables of C #, simple data types, arrays, and strings. We also learned how to form an expression with a C # operator.