C # basic-variables and constants (1 ),
I. Basic concepts of variables:
Variables are used to store specific types of data and change the data values stored in variables at any time. Variables have names, types, and values. Variable values can change. Before using a variable, you must declare the variable, that is, specify the type and name of the variable.
1. Variable type:
Variable types are defined as values and reference types.
2. Variable type;
1. Value Type features:
<! -- [If! SupportLists] --> ① <! -- [Endif] --> value type variables are stored in the stack;
<! -- [If! SupportLists] --> ② <! -- [Endif] --> when you access a value type variable, you generally directly access its instance;
<! -- [If! SupportLists] --> ③ <! -- [Endif] --> each value type variable has its own data copy, so operations on a value type variable will not affect other variables;
<! -- [If! SupportLists] --> ④ <! -- [Endif] --> When copying a value type variable, the copied value is the variable value, not the variable address;
<! -- [If! SupportLists] --> ⑤ <! -- [Endif] --> A value type variable cannot be NULL and must have a definite value.
The value type is inherited from the System. ValueType class.
2. Integer: the integer type indicates an integer without a decimal point.
Type |
Description |
Range |
Sbyte |
8-bit signed integer |
The value range is 128 ~ Between 127 |
Short |
16-bit signed integer |
The value range is-32,768 ~ Between 32,767 |
Int |
32-bit signed integer |
The value range is-2,147,483,648 ~ Between 2,147,483,647 |
Long |
64-bit signed integer |
The value range is-9,223,372,036,854,775,808 ~ Between 9,223,372,036,854,775,807 |
Byte |
8-bit unsigned integer |
The value range is 0 ~ Between 255 |
Ushort |
16-bit unsigned integer |
The value range is 0 ~ Between 65,535 |
Uint |
32-bit unsigned integer |
The value range is 0 ~ Between 4,294,967,295 |
Ulong |
64-bit unsigned integer |
The value range is 0 ~ Between 18,446,744,073,709,551,615 |
Create a console application, declare the int-type variable ls, initialize it to 927, and initialize the shj variable for the byte type as 255. Output
1 statci void Main (string [] arge) 2 {3 int ls = 927; // declare an int type variable ls4 byte shj = 255; // declare a byte type variable shj5 Console. writeLine ("ls = {0}, shj = {1}" + ls, shj); // output 6 Console. readLine (); 7}
If the value of shj is 266, the byte value ranges from 0 ~ 255
3. floating point type: floating point type variables are mainly used to process numerical data with decimals. Floating Point variables include float and double. Their difference lies in the value range and accuracy.
Type |
Description |
Range |
Float |
Accurate to 7 digits |
The value range is 1.5x10 ^-45 ~ Between 3.4x10 ^ 38 |
Double |
Accurate to 15 ~ 16-digit |
Value Range: 5.0x10 ^-324 ~ Between 1.7x10 ^ 308 |
If no value is set, the numeric value containing the decimal point is considered to be of the double type. If no value is specified, this value is of the double type. If you want to process a value of the float type, you need to forcibly convert it to the float type using f or F.
1 double MyDou = 927d; // use d to forcibly convert to double2 double mudou = 112D; // use D to forcibly convert to double
To forcibly specify the value type as double, use D or d.
1 double MyDou = 927d; // use d to forcibly convert to double2 double mudou = 112D; // use D to forcibly convert to double
<! -- [If! SupportLists] --> 3. <! -- [Endif] --> boolean type: boolean type is mainly used to indicate true/false values. A boolean type variable can only be true or false, other values cannot be specified to boolean variables. boolean variables cannot be converted to other types.
Assign 927 to the Boolean variable x
1 Bool x=927;
The value assignment is incorrect. The constant value 927 cannot be converted to bool.
Iii. Reference Type:
1. The reference type is the primary object type data for building a C # application. The pre-defined object type creates an object instance with new and stores it in the stack.
2. Reference Type features:
<! -- [If! SupportLists] --> ① <! -- [Endif] --> memory must be allocated for referenced type variables in the managed heap;
<! -- [If! SupportLists] --> ② <! -- [Endif] --> you must use the new keyword to create a reference type variable;
<! -- [If! SupportLists] --> ③ <! -- [Endif] --> each object allocated in the managed heap has an additional member associated with it, and these members must be initialized;
<! -- [If! SupportLists] --> ④ <! -- [Endif] --> reference type variable roommate garbage collection mechanism for management;
⑤ Multiple referenced variables can reference the same object. In this case, operations on one variable will affect the same object referenced by another variable.
⑥ The value before the reference type is assigned a value is NULL.
All referred to as "classes" are reference types, including classes, interfaces, arrays, and delegation.
Create a console application, create a class C in it, create a field Value in this class, and initialize it to 0. create the reference type variable of the class through new at other locations, and finally output the variable.
1 class Program 2 {3 class C // create a class C 4 {5 public int Value = 0; // declare a public int type variable Value 6} 7 static void Main (string [] args) 8 {9 int v1 = 0; // declare an int type variable v1, and initialize it to 010 int v2 = 1; // declare an int type variable v2 and assign v1 v211 v2 = 927; // assign the variable V2 to 92712 C r1 = new C () again; // use the new keyword to create the reference object 13 C r2 = r1; // set r1 = r214 r2.Value = 112; // set the value of r2 to 15 Console. writeLine ("Values; {0}, {1}", v1, v2); // you can specify 16 Console variables for output v1 and v2. writeLine ("refs; {0}, {1}", r1.Value, r2.Value); // output the value of the reference type object 17 Console. readLine (); 18} 19}