[Ildasm boxing] What I learned from the difference between struct and class

Source: Internet
Author: User
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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.