1. Difference Between struct and class: (1) structure is a value type, not a reference type. (2) The structure can continue with the interface, but cannot inherit the class or structure. (3) The constructor of a structure works in different ways. Only the constructor with parameters can be declared, and The Destructor cannot be declared. (4) You can specify how fields are laid out in memory.
(5) use. You can use either of the following methods. But remember: In method 1, "users user;" does not call the constructor to initialize the member variable !! So, in general, it is better to use method 2! Msdn words:
TheStructType is suitable for representing lightweight objects such as point, rectangle, and color. although it is possible to represent a point as a class, a struct is more efficient in some scenarios. for example, if you declare an array of 1000 point objects, you will allocate additional memory for referencing each object. in this case, the struct is less expensive.
It is an error to declare a default (parameterless) constructor for a struct. A default constructor is always provided to initialize the struct members to their default values.
It is an error to initialize an instance field in a struct.
When you create a struct object usingNewOperator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without usingNewOperator. If you do not useNew, The fields will remain unassigned and the object cannot be used until all of the fields are initialized.
There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. structs, however, inherit from the base classObject. A struct can implement interfaces, and it does that exactly as classes do.
Method 1:
Static void main ()
{
Users user;
User. Username = "Zhang Sanfeng ";
User. usersex = "female ";
User. userage = 18;
User. getinfo ();
}
Method 2:
Static void main ()
{
Users user = new users ("Zhang Sanfeng", "male", 29 );
User. getinfo ();
}
2. view the Assembly, view disassembly directly in vs; view Il, open commandprompt: ildasm, and then open any. net assembly, you can view what reflection can see and the Il code.
3. struct can only be allocated to the stack. However, do not forget that any value type can be changed to an object allocated to heap through boxing.