There are many differences between the value type and the reference type. For details, refer to examples.
The structure is a value type. First, the NEW structure has different class types,
The class type uses new to call the constructor to allocate memory storage space in the managed heap and return its address. The structure type uses new to call the constructor initialization field instead of allocating the managed heap memory. The constructor of the structure either has parameters and cannot have no parameters. It should be added by the system by itself without parameters. Even if there is another constructor, if you declare a non-parameter constructor in the structure, it will cause a compilation error.
The structure is that the value type is stored in the stack. The value type variables include the variable address and variable content, so they can be directly like int, double .. for other purposes, do not use new to use new. It is just a little convenient to initialize the field, but you need to add your own constructor.
Struct my
{
Int x;
Int y;
My (int x, int y)
{This. x = x; this. y = y}
}
My mytest = new my (10, 10 );
In this way, both mytest. x and mytest. y are initialized to 10;
The structure can also be initialized like this
My mytest; current mytest Data Value Type Variable
Mytest. x = XX;
The reference type does not allow this. The reference type stores its address, so you need to use the new return address and allocate space.
Fields cannot be initialized in the structure,
For example
Struct my
{
Int x = 10;
}
Compilation errors will occur because my is a data type and does not occupy storage space. It can be initialized and used only when variables are defined.
Struct my
{
Int x;
}
My mytest;
Mytest. x = 10;
Int x; and my mytest;
The same is true.
The x and struct values are of the value type.
The Value Type I will introduce here is a common concern.
First, we need to know that each value type has an implicit default constructor to initialize the default value of this type.
For example, int I = new int () <=> Int32 I = new Int32 () <=> int I = 0 <=> Int32 I = 0;
When the new operator is used, a default constructor of a specific type is called and the variable is assigned the default value.
Note that the new operation of struct does not execute the memory allocation operation (different from the new operation of class ),
The new operation only initializes data.
Let's look at an example.
Int I = new int ();
Console. WriteLine (I );
However, the following code causes the compiler error CS0165 because it does not use new and tries to use objects that have not been initialized:
Int I;
Console. WriteLine (I );
Check again
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace fanxing
...{
Public struct Address
...{
Private string _ city;
Private string _ province;
Private int _ zipCode;
// Private int _ zipCode = 0; the structure cannot contain the initial value of the Instance field.
Public Address (string city, string province, int zipCode)
...{
_ City = city;
_ Province = province;
_ ZipCode = zipCode;
}
/** // * Public Address (string city, string province)
{
_ City = city;
_ Province = province;
} If a constructor is created with parameters, all domain fields must be copied */
// Public Address ()
//{
// A message is displayed, indicating that the structure cannot contain any non-parameter constructor.
//}
Public string City
...{
Get... {return _ city ;}
}
Public string Province
...{
Get... {return _ province ;}
}
Public int ZipCode
...{
Get... {return _ zipCode ;}
}
}
Public class Address1
...{
Private string _ city;
Private string _ province;
// Private int _ zipCode;
Private int _ zipCode = 0;
Public static int aaa = 8;
Static Address1 ()
...{
Aaa = 10;
Console. Write (aaa );
}
Public Address1 ()
...{
}
Public Address1 (string city, string province)
...{
_ City = city;
_ Province = province;
Aaa = 20;
Console. Write (aaa );
}
Public string City
...{
Get... {return _ city ;}
}
Public string Province
...{
Get... {return _ province ;}
}
Public int ZipCode
...{
Get... {return _ zipCode ;}
}
}
Class Application1234
...{
Static void Main (string [] args)
...{
Address;
// Int I;
// Console. WriteLine (I );
// Console. Write (a. ZipCode); // if no new value exists, a prompt is displayed for using the unassigned local variable.
A = new Address ();
// Console. Write (a. City + a. ZipCode); // null + 0
Console. Write (Address1.aaa );
// Here we will introduce the usage of static constructor,
// Whether it is instantiating or calling static variables, the static constructor calls only once, and it is called before the non-static constructor.
Address1 B = new Address1 ("a", "B ");
// Console. Write (B. City + B. ZipCode); // null + 0
Console. Read ();
}
}
}
Similarly, for struct, initialization must be performed through new, but structs cannot contain an explicit default constructor because the compiler will automatically provide a constructor. This constructor initializes each field in struct to the default value (by default, the CLR automatically sets the value type to 0 in both the class and structure, and the default reference type is null ), this default constructor is called only when struct is instantiated with new.
Next, pay attention to this problem: structs-based objects (including all built-in numeric types) can be initialized or assigned values for use, as shown in the following example:
Int a = 44; // Initialize the value type...
Int B;
B = 33; // Or assign it before using it.
Console. WriteLine ("{0}, {1}", a, B); that is, calling the default constructor for the value type is not required.
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/xiaolei1982/archive/2008/05/08/2416544.aspx