This directory
Comparison with the structure of the instance of the class
The difference between class and structure
How to choose the structure or class
1. Comparison with the structure of the sample class:
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 the procedure:
public class Test
(
public static ovid Main
(
Person Myperson = new Person / / declare structure
TestTime Mytime = New TestTime / / declare class
)
)
From the example above we can see, the class declaration and the structure is very similar statements, only qualifier followed struct Haishi class differentiated and when used to define a new structure and definition of new class methods are very similar. Then the specific type and structure is the difference between what?
2. The difference between class and structure
1. Value types and reference types
Structure is a value type: Value types are allocated on the stack address, all the basic types are structure types, such as: int corresponding System.int32 structure, string corresponding system.string structure, by using the structure can create more value types
Classes are reference types: reference types allocated on the heap address
The execution efficiency of the stack implementation efficient than the heap, stack, but limited resources, the logic is not suitable for handling large complex objects. Therefore, as the base type of treatment to treat the structure of small objects, while handling a business logic class
Because the structure is a value type so assignment between structures can create new structure, but the classes are reference types, type assignment is duplicated between the reference
Note:
1. Although the structure of different types and classes, but their base type is an object (object), c # in all types of base types are object
2. Although the structure of the initialization operator also uses the New Structure of the object, but is still allocated on the stack instead of heap, if you do not use the "new" (new), then initialize all the fields before the field will remain unassigned state, and the object is not available
2. Succession
Structure: not from another structure or class inheritance, in itself and can not be inherited, although the structure with sealed statement is not clear, but the structure is implicitly sealed.
Categories: fully scalable, unless shown otherwise, the statement sealed class can inherit other classes and interfaces can also be self-succession
Note: Although the structure can not be inherited, but the structure can be inherited interfaces, methods, and interfaces inherit the same class
For example: Structures of Interfaces
interface IImage
(
void Paint ();
)
struct Picture: IImage
(
public void Paint ()
(
/ / Painting code goes here
)
private int x, y, z; / / other struct members
)
3. Internal structure:
Structure:
No default constructor, but you can add constructor
No destructor
Not abstract and sealed (because they can not inherit)
Not have the protected modifier
Can not use the new initialization
Initialize instance fields in the structure is wrong
Category:
A default constructor
There destructor
You can use abstract and sealed
Are protected modifiers
Must use the new initialization
3. How to choose the structure or class
Discussed the structure and the similarities and differences in class, the following discussion of how to choose the structure or class:
1. Stack space is limited, the number of logical objects, create a class to create structures better than
2. Structure expressed as points, rectangles and color of such lightweight objects, for example, if the statement contains an array of 1000 Point objects, each object will be allocated for the reference to additional memory. In this case, lower cost structure.
3. In the performance level of abstraction and multi-object level, the class is the best option
4. In most cases just a few of the types of data, structure the best option
Recommended links:
How To Locate And Win The Consumer's Mind
MKV to PSP
Best E-Commerce
Log Analysers Comparison
2.0 HOMOGENIZATION
astronomy a lifelong HOBBY
FOREIGN Trade before sending samples: judgments, proofing and expenses
FLV to Zune
MAC-based access control list XIANGJIE
Modeling With UML To Note Problems
JSP And EJB Interaction
Teaching And TRAINING Tools Report
AVI To MKV
Enjoy it: lots to talk about topics for undying domain
BT Has Been Closed, We See What?
No comments:
Post a Comment