Directory
Class and structure instance comparison
Differences between classes and structures
How to Select structure or Class
I. Comparison of classes and structures:
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
}
}
Call process:
Public class Test
{
Public static ovid Main
{
Person Myperson = new Person // declaration Structure
TestTime Mytime = New TestTime // Declaration class
}
}
From the above example, we can see that the declaration of the class and the declaration of the structure are very similar, but the difference between struct and class is after the qualifier, and when used, similar methods are used to define new structures and new classes. What are the specific differences between classes and structures?
Ii. Differences between classes and structures
1. Value Type and reference type
Structure is value type: value type is allocated on the stack. All base types are structure types. For example, int corresponds to System. int32 structure. string corresponds to system. string Structure. You can create more value types by using the structure.
Class is a reference type: the reference type is allocated on the stack.