Directory
First, let's make a question. This question is of the same type as this one. However, this is about the structure and Class. Its essence is the use of the value type and reference type.
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace ConsoleApplication1
{
Class MyClass
{
Public int val;
}
Struct myStruct
{
Public int val;
}
Class Program
{
Static void Main (string [] args)
{
MyClass objectA = new MyClass ();
MyClass objectB = objectA;
ObjectA. val = 10;
ObjectB. val = 20;
MyStruct structA = new myStruct ();
MyStruct structB = structA;
StructA. val = 30;
StructB. val = 40;
Console. WriteLine (objectA. val );
Console. WriteLine (objectB. val );
Console. WriteLine (structA. val );
Console. WriteLine (structB. val );
}
}
}
For output results, see
For the best distinction, you can make the difference from the following aspects:
1. Value Type and reference type
Structure is value type: value type is allocated on Stack
Class is a reference type: the reference type is allocated on the stack.
Stack execution efficiency is higher than stack execution efficiency, but stack resources are limited, not suitable for processing large logical complex objects.
Therefore, structure processing is a small object to be treated as a base type, while classes process a certain business logic.
Because the structure is a value type, a value can be assigned between structures to create a new structure, while classes are reference types. The value assignment between classes is just a copy reference (if you do not understand it, please advise)
2. Inheritance
Structure: it cannot be inherited from another structure or class. Although the structure is not explicitly declared using sealed, the structure is implicit sealed.
Class: fully scalable. Unless the declared sealed is displayed, the class can inherit other classes and interfaces, and its own can also be inherited.
Note:Although the structure cannot be inherited, the structure can inherit interfaces. methods are the same as class inheritance interfaces.
3. Internal Structure
Structure:
No default constructor exists, but you can add constructor.
No destructor
No abstract and sealed (because it cannot be inherited)
The protected modifier cannot exist.
You do not need to use new for initialization.
It is incorrect to initialize the instance field in the structure.
Class:
Default constructor available
Destructor
Abstract and sealed can be used.
There is a protected Modifier
New must be used for initialization.
3. How to Select a structure or a class
After discussing the similarities and differences between structures and classes, we will discuss how to choose a structure or a class:
1. The stack space is limited. For a large number of logical objects, creating classes is better than creating structures.
2. structure indicates lightweight objects such as vertices, rectangles, and colors. For example, if an array containing 1000 vertex objects is declared,
Additional memory will be allocated for each referenced object. In this case, the structure cost is low.
3. Classes are the best choice for presentation of abstract and multi-level object Layers
4. In most cases, this type is the best choice for structure when it is only some data.
Reference resources:
Http://ty.cquc.edu.cn/show.aspx? Id = 47108 & cid = 17
Http://blog.csdn.net/zyl910/article/details/6788417 (this article is very esoteric, not two brushes are not clear)