1. Variables
Variable refers to the amount of changes that can occur at any time during program running.
Syntax: data type variable name;
For example, string stra = "this is a string ";
2. Constants
It is also called a constant, which is a constant or immutable value or data item.
Syntax: const data type constant name = constant value;
Example: const string strconst = "constant ";
3. Data Type:
In C #, there are two data types: Value Type and reference type.
The value type indicates the actual data, but stores the value in the memory. The value types are all stored in the stack, including: int, Char, float, double, bool, etc, there are also structures and enumerations.
Reference type, which indicates a pointer or reference to data, including the address of the object in the memory heap. If it is null, it indicates that no object is referenced, including: Class, interface, array, String, delegate, object.
The C # Alias and the C # Alias of the pre-defined value type are very similar. The first type is the object class (the C # Alias is object, and the O is lowercase ). This is the final base class of all value types and reference types. Because all types are derived from objects, any type can be converted to the object type, or even the value type can be converted. All value types are derived from the reference type. The second type is the string class. A string represents a fixed sequence of Unicode characters. A string is like a value type in many ways, rather than a reference type.
4. C # variable type conversion:
Conversion between string and Value Type:
Type. parse (string), the return value is the corresponding type.
Convert. To type (parameter). There are multiple parameters, and the return value is type.
Example:
- Static void main ()
- {
- Int Inti = int. parse ("1"); // type. parse (string)
- Int intj = convert. toint32 ("1"); // convert. To type (parameter)
- }
Forced C # variable type conversion:
The conversion from a small data type to a large data type is called extended conversion. Otherwise, it is called contraction conversion. The compiler can perform implicit extended conversions. Explicit mandatory conversions must be performed for the contract conversions. Because the conversion will cause data loss, we need to check whether the actual value exceeds the range of the target type before conversion. Another method is to use the checked operator. If data is lost during conversion, an error is thrown.
For example:
- Static VoidMain ()
- {
- Int Inti = 7; // a 32-bit integer
- Long Intl = 8; // a 64-bit integer
- INTl = Inti; // implicit conversion is used when int type is converted from small to long type.
- Inti = (INT) Intl; // when converting a large type of logn to a small type of int, you must strengthen the conversion (INT)
- }
Packing and unpacking
Definition: the process of converting a value type to an object is called packing.
Because the storage methods of value types and reference types are different in the memory, our understanding of the conversion process is somewhat complicated. The actual syntax is no different from that of forced type conversion, our image is called packing and unpacking. the box breaking restriction is actually very understandable, because no one can remove the grapes from the box containing apple.
- Static VoidMain ()
- {
- Int J = 4; // You can explicitly pack a value in the following normal conversion Syntax:
- Object OJB = (object) J;
- Int K = (INT) OBJ; // use the same C # variable type conversion syntax to unbox the boxed variables and return them to the value type:
- // There are some restrictions on the binning operation. You can only unpack variables explicitly. Restrictions in normal forced conversions also apply here. For example, if we pack a long value into an object, we cannot split the object into an int value. Although we can explicitly convert long to int after unpacking:
- Long x = 1000;
- Object OBJ = (object) X;
- Int I = (INT) (long) OBJ );
- }
Learning from http://developer.51cto.com/art/200908/143618.htm