Variables | Tutorial This section describes the expressions, types, and variables of the C # language. This lesson aims to achieve the following objectives:
1. Know what a "variable" is
2. Learn simple types of C #
3. Have a preliminary understanding of C # expressions
4. Understand what a string type
5. Learn how to use arrays
"Variable" is just the location where the data is stored. You can store the data in it or take it out as part of a C # expression. The meaning of the data stored in the variable is controlled by the type.
C # is a strong type (??? ) in the language. In this way, all operations on a variable are performed on the type of the variable. In order to ensure the legality and consistency of the data stored in the variables, there are corresponding rules for the operation of 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 shows the Boolean value: Boolean.cs
Using System;
Class Booleans {
public static void Main () {
BOOL content = true;
BOOL nocontent = false;
Console.WriteLine ("It is {0}"-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 exported to the console as part of the sentence. The value of the "bool" type is either True or false. The results of the program operation are 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 various integer types, the size of the byte and the range of numbers that can be represented.
Type-bit range
SByte 8-128 to 127
BYTE 8 0 to 255
Short 16-32768 to 32767
UShort 0 to 65535
int 32-2147483648 to 2147483647
UINT 0 to 4294967295
Long 64-9223372036854775808 to 9223372036854775807
ULONG 0 to 18446744073709551615
Char 0 to 65535
These types are appropriate in addition to the character type when evaluating integers. The character type represents a Unicode character. As you can see from the table above, you can choose the type that fits your needs.
3. The following table shows the single precision type, the double type and the decimal type data, the byte, the precision, and the range of numbers that can be represented.
Type-bit precision range
Float 7 digits 1.5 x 10-45 to 3.4 x 1038
Double 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 do the operations involving fractions, you need to use a real type, however, the decimal type may be the best choice for financial and financial data calculations.
4. After the expression is calculated, the result can be obtained. These expressions place the variables and operators together in the statement. The following table lists the operators, precedence, and binding that C # allows.
Classification operator Binding
Primary (x) x.y f (x) a[x] x + + x--new typeof sizeof checked unchecked left
Monocular +-! ~ ++x--x (T) x left
multiplication etc */% Left
Addition etc +-left
Shift << >> Left
Relationship < > <= >= is left
Equal = =!= Right
Logic and & Left
Logical XOR or ^ left
Logic or | Left
Conditions with && left
Condition or | | Left
Conditions?: Right
Assignment etc = *=/=%= = = <<= >>= &= ^= |= Right
The left binding means that the operator is performed from left to right. The right combination means that all operations are done from right to left, such as the assignment operator, until the right-hand side of the calculation comes to the left side of the variable.
2. Listing 1-2. Monocular 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);
}
}
Description
1. When the expression is calculated, after adding one and after the subtraction operator, return its value, and then add one or minus one operation. When you use the preceding plus and minus operators to perform an operation, you first increment or subtract one, and then return the resulting value.
2. In Listing 1-2, the variable unary is initialized to 0, the value of "unary" is added 1, and the value 1 is assigned to the "preincrement" variable when the ++x operation is performed. In the--x operation, the value of "unary" is reduced to 0, and the value 0 is assigned to the "predecrement" variable.
3. In the X-operation, first "unary" value 0 assigned to the "postdecrement" variable, and then "unary" reduced to 1. X + + operations, the first "unary" value-1 assigned to the "postincrement" variable, and then "unary" plus 1, so that "unary" variable now has a value of 0.
4. The variable "bitnot" initial value is 0, carries on the bitwise to take the inverse operation, in this example, the number 0 is expressed as binary "00000000", after the bitwise reverse turns to-1, the binary system is expressed as "11111111".
5. Understand the expression "(sbyte)" (~bitnot), any operation on type sbyte, Byte, short, or ushort type of data, return the result is an integer. To assign a value to a bitnot variable, we must use the cast (type) operator (coercion type conversion) where type represents the type you want to convert to (in this case, sbyte). The cast operator must be particularly cautious when converting data of a large range type to data of a small range type, because there is a risk of losing data at this time. In general, it is not a problem to assign small types of data to large types of variables, because variables of a large range of data types have enough space to hold small types of data. Note This danger also occurs when cast operations are performed between signed and unsigned types. Many beginner programming tutorials provide a good explanation of the bit representation of variables, as well as the dangers of direct cast operations.
Logical NON (!) Operators can handle Boolean variable values. In this case, the "lognot" variable changes from false to true.
The output 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);
}
}
Description
Listing 1-3 illustrates several examples of the two-dollar operator. Addition (+), subtraction (-), multiplication (*) and division (/) are the results of the arithmetic that we normally do.
Because the "floatresult" variable is a floating-point operation type, the integer variable "x" and "Y" are cast to a floating-point type to compute the Floatresult.
Here is a remainder operator, divided by two operands, and returns the remainder.
The last statement gives another form of assignment, where the (+ =) operator is used. Whenever you use the (+ =) operator, the binary operator should be evaluated on both sides of the operator, and then assign the value to the left parameter. This statement corresponds to "result = result + X" and returns the same value.
One of the more frequently used types you see in the previous course is the string type. The string type is composed of Unicode-encoded characters enclosed in quotation marks. For example, "This is a string."
Another type of data is an array. An array can be viewed as a collection of elements of the same type. When declaring an array, you specify the type name, the array name, the dimensions, and the size of the group.
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;
Listing 1-4 shows the various implementations of the array. The first example is the "myints" array, which is initialized at the same time as the declaration.
Then there is a two-dimensional array, which can be interpreted as an array of arrays. We need to use the "new" operator to instantiate the size of the initial array, and then use the new operator for each of the child arrays.
The third example is a two-dimensional array. Arrays can be multidimensional, each dimension can be separated by commas, and must be instantiated with the "new" operator.
Finally, a one-dimensional string array is defined.
In each case, access to the data element can be done by referencing the location of the element (subscript). The size of an array can be any integer value. Its subscript starts at 0.
Summary
So far, you've learned about C # variables, simple data types, arrays, and strings. We also learned how to use C # 's operators to make an expression.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.