Friday, October 8, 2010

c # and class differences in the structure


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. Example of class and structure comparison:

Structure Example:

public struct Person

(

string Name;

int height;

int weight

public bool overWeight ()

(

file / / implement something

)

)

Class example:

public class TestTime

(

int hours;

int minutes;

int seconds;

public void passtime ()

(

file / / implementation of behavior

)
)

Call the procedure:

public class Test

(

public static ovid Main

(

Person Myperson = new Person file / / declare structure

TestTime Mytime = New TestTime file / / declare class

)

)

From the example above we can see, the class declaration and the structure is very similar statements, only a qualifier followed by the struct or class distinction, and used to define the new structure and definition of new classes of 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




[Next]



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:



Evaluation Browser Tools



Motorola recall: a beautiful mistake



Compare Reference Tools



Production OF football Photoshop 7.0



WMV to MOV



MPEG4 To WMV



quot failed to start because d3dx9 37 dll Quot



Teach you QUICKLY learn how to record "unknown error" occurs



Li ZHONG: encourage employees to as "General"



RUP Tailoring principles and cutting process



XML Or CSS TOOLS Guide



Easy to use Remote Computing



Flash TO MPEG



Dell Digital denial distribution relationship With HP induced breakdown



Do they only quick Ctrl + Alt + End defects more quickly shutdown



L'OREAL portrait of "Edgar" Human resources together to create



No comments:

Post a Comment