drill down into C # data types
1. Value types and reference types
1. Value type: Store data type directly in the stack
question: It is known that Li Ming and Zhang Hao last year's height were 170cm, and this year Li Ming height to 180cm, Zhang Hao unchanged, output last year and this year two people's height
Code:
int heightzhang =;
int heightli = Heightzhang;
Console.WriteLine ("Zhang Hao Height: {0} Li Ming's height: {1}", Heightzhang,heightli);
Heightli = n;
Console.WriteLine ("Zhang Hao Height: {0} Li Ming's height: {1}", Heightzhang, Heightli);
console.readline ();
Summary: Value types: Different changes will allocate different storage space, storage space is stored in the variable value, changing the value of one variable does not affect the value of another variable
2. Reference type: Store data types directly in the heap
1. Problem: It is known that Li Ming and Zhang Hao last year's height of 170cm weight of 60kg, and this year Li Ming height to 180cm weight long to 70kg, Zhang Hao unchanged, output last year and this year two people's height
Code:
int[] li = {170,60};
int[] Zhang = li;
Console.WriteLine ("Last year-Zhang Hao height: {0} Weight: {1} li Ming height: {2} Weight: {3}", Zhang[0],zhang[1],li[0],li[1]);
li[0] =;
li[1] =;
Console.WriteLine ("Last year-Zhang Hao height: {0} Weight: {1} li Ming height: {2} Weight: {3}", Zhang[0], zhang[1], li[0], li[1]);
console.readline ();
Why does the value of Zhang Hao and Li Ming change?
First, the system allocates a space to create an array for storing information, and the identifier Li is just a reference to the array created in memory, and then the application refers to Li to assign the value to Zhang because Zhang does not allocate a storage space,
Instead, it uses the same storage space as Li, so the latter program also modifies the Zhang Hao information as it modifies the height and weight of the dawn.
The picture outlines the process of change:
Summary: For reference types, assignment is to pass a meta-object reference to another reference. For arrays, when an array is assigned to another reference, the two music only wants the same array, that is, to use only one piece of storage space
1. Structure
1. Definition of structure
access modifier struct struct name
{
//Structural body
}
the definition of a structure has the following characteristics:
There can be fields in the structure
when defined, a field in a struct cannot be assigned a value
2. Use of Structures
You can directly define the object of the structure without new.
after declaring the resulting object, you must assign an initial value to the member of the result
Summary: The structure is a value type, declaring the structure variable to store a new copy of the structure, and the system to open a new piece of storage space, so the more structure used more storage space consumed
2. Crating and Unpacking
the process of defining the conversion of a value type to a reference type is called boxing, which in turn is a unboxing.
1. Packing
When I change the value of I, because I is a value type, I can only change its own value, and cannot modify the type of the reference type object. Can be understood as creating an object instance and copying the value of I to object
2. Unpacking
The type I is boxed first, then the converted reference type O for unpacking, remember that when unpacking, the value of the converted reference type must be able to be converted to the target value type.
Experience: In the actual development, the unnecessary packing and unpacking should be minimized, because the storage mode of the two is different and the performance loss is great during the conversion.
Summary
* Basic data types, such as Integer, float, bool, and struct are value types; arrays, interfaces, and classes belong to reference types
* structure is a value type data, can be regarded as a lightweight class, when used can not use the new
* value type conversion To reference type is called boxing, which is called unboxing
Drill down into C # data types