ronniefalk

September 30, 2010

c # and class differences in the structure

Filed under: Uncategorized

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:

AVI to MOV

Ad Blockers Comparison

How To Convert Flash To Psp

Computex Taipei will become the focus of the opening of low-cost PC

How GIS abstract surface features of space?

MPG To Flash

Like Facebook wants To rule the world?

Delphi beginners reference (b)

DOS, Also Use Flash Tips

brief Audio And Multimedia

Programming books from the ENTER_FRAME ActionScript3 Speaking

3GP to MPEG

“High Family” how to “preserve and increase the value?”

Comments »

The URI to TrackBack this entry is: http://ronniefalkus.blogsome.com/2010/09/30/c-and-class-differences-in-the-structure/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Gary Rogers