|
-
Oct 28th, 2003, 01:15 AM
#1
Value Types and Classes [resolved]
What is the difference between these two? From the definition of a Value Type
Value types are stored as primitive data types, but they can contain fields, properties, events, and both static and nonstatic methods. Value types do not carry the overhead of an object that is being held in memory.
To me, it seems like a Value Type is a class. What then, is the difference? I'd like a better explanation than the one I've found here.
Last edited by mendhak; Oct 29th, 2003 at 04:09 AM.
-
Oct 28th, 2003, 02:06 AM
#2
I think reference types are stores on the heap.... bache! 
I just give you an example. this is the msot important thing you should care about, figure out the details on your own
Dim A,B as myClass
B = new myClass()
A=B
' if you change A, B will be changed also. If you change B, A will be changed also, cuz myClass is a class and class is a reference type!
Dim A, B as myStruct
A=B
B.foo = "blah"
' A.foo hasn't changed. Structures are value types
uuh and classes are reference types. Most things are reference types, besides the primative data types and some other things
remember one thing though, if you have an array of value types (ie, an array of ints), the array itself is a reference type.
dim arr() as integer
arr is a reference type
arr(0) is a value type
Last edited by MrPolite; Oct 28th, 2003 at 02:11 AM.
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Oct 28th, 2003, 02:51 AM
#3
Frenzied Member
Originally posted by MrPolite
Most things are reference types, besides the primative data types and some other things
No, among primitive data types only String is a Reference type, all others are Value Types and 'Object' can act as both.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Oct 28th, 2003, 08:45 AM
#4
PowerPoster
Types can be divided into two types:
The main difference between the two is where they are allocated in memory. Value types are stored in an application specific location of memory, called the stack. Reference types on the other hand are stored on the managed heap. The CLR, specifically the garbage collector, monitors the heap for objects that are no longer reference, and releases their memory and de-allocated the memory when necessary.
All objects (including value types) ultimately derive from System.Object. When a value type needs to act like an object, boxing occurs. Boxing allows a value type to be treated as a reference type The quote you posted is describing a structure, which is in fact a value type (somewhat of a lightweight class).
However, unlike reference types, you can not derive a class from a value type, but you can implement one or more interfaces.
-
Oct 28th, 2003, 08:53 AM
#5
Addicted Member
To summarise...
Value types:
--are allocated on the stack
--are copied when passed as arguments
--cannot be inherited
Reference type:
--allocated on the stack
--can be inherited (unless marked as sealed)
--only addresses are passed as args, not the value
like Lunatic3 says, ultimately 'object' can act as both but there is a boxing overhead in switching between the 2.
Cheers...
-
Oct 28th, 2003, 08:54 AM
#6
Addicted Member
beat me to the punch Lethal...
-
Oct 28th, 2003, 10:47 PM
#7
Thanks for the explanation guys. I've understood.
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
|