|
-
Oct 23rd, 2010, 04:50 AM
#1
Classes VS Structures
Hello,
I know that Structures are value types and classes are reference type, i know that since structures are stored in stack they are faster to use i also understands that structures can not use the Inheritance concept
but still in most examples i see i always see that the authors use classes when building something like BBL library, if structures is light-weight compare to classes and i don't need it to be inherited should i use structure instead of class?
is there a rule of when to use class and when to use structure ?
Thanks.
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Oct 23rd, 2010, 05:42 AM
#2
Re: Classes VS Structures
The vast majority of the types you create should be classes. Structures are faster to access because they are stored on the stack, but that also means that any time you assign the value of a variable containing a structure to another variable, you copy the entire object.
As a rule of thumb, value types should never be more than 16 bytes in size. A standard reference type variable is 4 bytes. An Integer is 4 bytes. A Long is 8 bytes. As such, if you were going to define a type with three fields: one a reference type (that includes String), one an Integer and one a Long, that would be as big as a structure should be. If you wanted to add another field then you should consider a class rather than a structure.
Structures should only be used to represent very simple type. In many cases, a structure contains just a single field and then the properties and method of the type interpret that one value. A classic example is the DateTime structure. Internally, its just a single number that occupies 8 bytes:
 Originally Posted by MSDN
Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar. For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight. A DateTime value is always expressed in the context of an explicit or default calendar.
Generally, assume that new types that you create will be classes unless you have some specific reason for making them structures.
-
Oct 23rd, 2010, 06:34 AM
#3
Re: Classes VS Structures
Ok thanks for the info JM.
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|