Structure and class

xiaoxiao2021-03-06  142

In C , structural and classes are similar, and the members of the structure defaults to public types, and the members of the class defaults to private types. In C #, they are completely different types:

1: Type:

The structure is a value type, assigns an address on the stack, so the assignment between the structure can create a new structure; the class is a reference type, assigns an address on the heap, so the assignment is just a reply reference. The implementation efficiency of the stack is high than the implementation efficiency, but the resource of the stack is limited, not suitable for processing large logic complex objects, so structural processing as a small object treated by the base type, and the class handles a business logic.

2: Inheritance:

The structure cannot be inherited, except that they are derived from System.ValeType. However, the structure can inherit the interface, like a method and class inheritance interface. The class can be derived from a base class, or can be derived from any more interfaces.

3. Internal structure:

structure:

Provide unfolded default constructor and is not allowed, but can be added to the reference construct.

No destructor

No Virtual, Abstract and SeaD (because you can't inherit)

No protected modifier

The initialization instance field in the structure is wrong

class:

Default constructor

Have a patterned function

You can use Virtural, Abstract and Sealed

Protected modifier

Can initialize instance fields

4: Structure can specify how fields are layout in memory

转载请注明原文地址:https://www.9cbs.com/read-127223.html

New Post(0)