The development of programs is inseparable from the applications of variables and constants. When learning VB, we also have some knowledge about variables and constants. In fact, variables are used to store specific types of data, constants store unchanged data values.
I. Variables
1. Basic concepts:
The variable itself is used to store specific types of data. You can change the data values stored in the variable at any time as needed. Variables have names, types, and values. Similar to the class, variables must be declared before they are used, that is, the type and name of the specified variable.
2. Variable type:
In C #, variable types can be divided into two types according to definitions: one is the value type and the other is the reference type. The difference between the two lies in the data storage method. Variables of the value type directly store data, while the reference type stores reference of actual data. The program uses this reference to find the real data.
(1) Value Type
Value types include integer, floating point, and Boolean. Value Type variables are allocated in the stack, which is highly efficient.
A. Integer type:
The range of each data type is described here, so that we can select a suitable data type based on the value size, otherwise it may cause operation overflow.
For example, declare a byte type variable Android, initialize it to 255, and output it. The Code is as follows:
Static voidmain (string [] ARGs) {byte Android = 255; // declare a byte type variable Android console. writeline ("android = {0}", Android); // output the byte type variable Android console. readline ();}
Result:
When the byte type initial value is changed to 256, see the code:
Static void main (string [] ARGs) {byte Android = 256; // declare a byte type variable Android console. writeline ("android = {0}", Android); // output the byte type variable Android console. readline ();}
The following error occurs:
The two simple examples illustrate the importance of the data type range.
B. floating point type:
It mainly deals with numeric types containing decimals, including float and double.
If no special value is specified, the default value is double. To use a float variable, the value must be followed by f or F to perform forced conversion. For example:
Float themysum = 9.27f; // use F to forcibly specify Float Type Float themysum = 9.27f; // use F to forcibly specify Float Type
It is also similar to converting the float type to the double type. You only need to write the float as double and write f or F as D or D.
C. boolean type:
It is mainly used to indicate the true or false values. Most of them are used in flow control statements, such as loop statements or if statements.
(Supplement: if the global variable is not specified, the default values of the integer and floating point types are 0 and the boolean type is false .)
(2) Reference Type
All referred to as "classes" are reference types, including classes, interfaces, arrays, and delegation.
The following is an application that uses a reference type variable:
Class program {Class C // create a class C {public int value = 0; // declare a public int type variable value} static void main (string [] ARGs) {int V1 = 0; // declare a public int type variable V1 and initialize it to 0 int v2 = V1; // declare a public int type variable V2, assign V1 to V2 v2 = 927; // assign the variable V2 to 927 C R1 = new C () again; // use the new keyword to create the reference object C r2 = R1; // make R1 equal to R2 r2.value = 112; // set the value of the variable R2 console. writeline ("values: {0}, {1}", V1, V2); // output variables V1 and V2 console. writeline ("refs: {0}, {1}", r1.value, r2.value); // outputs the value console of the reference type object. readline ();}
The running result is:
2. Constants
A constant is a fixed amount, and the constant value is determined at compilation. In C #, the key word const is used to create a constant. When creating a constant, you must set its initial value. Once set, it cannot be modified.
For example:
Const doublepi = 3.1415926; // correct declaration method const int Myint; // error: No Initialization is performed when a constant is defined.
This blog introduces the variables and constants in detail. For the two types of variables: Value Type and reference type, I will use an example in this blog, this helps you better understand the differences between value types and reference types ~