类和结构体的区别(The difference between a class and a structure).doc
文本预览下载声明
类和结构体的区别(The difference between a class and a structure)
The difference between a class and a structure
Class and structure are very similar. Technically, class is a reference, and structure is numeric. Some people say very vividly
Class has actions, methods, members, a combination of organisms, and structure is a living organism,
Example comparison of class and structure:
Structure example:
Public struct Person
{
String Name;
Int height;
Int weight
Public, bool, overWeight ()
{
//implement something
}
}
Class example:
Public class TestTime
{
Int hours;
Int minutes;
Int seconds;
Public, void, passtime ()
{
//implementation of behavior
}
}
Calling procedure:
Public class Test
{
Public static Ovid Main
{
Person Myperson=new Person / / statement structure
TestTime Mytime=New TestTime / / class
}
}
When used, methods for defining new structures and defining new classes are also very similar. What are the specific differences between classes and structures?
The difference between two categories and structures
1. value types and reference types
The structure is a value type: value assigned address type on the stack, the base type is all structure types, for example: int System.int32 string system.string the corresponding structure, the corresponding structure, through the use of the structure can create more value types
Class is a reference type: the reference type assigns the address on the heap
The stack performs more efficiently than the stack does, but the stack has limited resources and is not suitable for handling large, complex objects. So the structure is treated as a small object treated by the base type, and the class handles a business logic
Because the structure is a value type, assignments between structures can create new structures, and classes are reference types; assignments between classes are just copy references
Note:
1., although the structure is different from the class type, their base type is object, and all types of base types in c# are object
2.
显示全部