Structure vs Class 
Author Message
 Structure vs Class

What is the main difference between Structure and Class? And when to use
Structure and when to use Class?


Sat, 14 May 2005 10:07:13 GMT  
 Structure vs Class
Ciu Pek Thong,
A Structure is a value type. Value types exist on the stack, when you pass a
Value type 'ByVal' you are getting a copy of the value, two pieces of the
same datum exist. Or when you assign one value type variable to another a
copy of the value is made.

A Class is a reference type. Reference types exist on the heap, when you
pass a Reference type 'ByVal' you are getting a copy of the reference, a
single object exists on the heap. Or when you assign one reference type
variable to another a copy of the reference is made, there is still only one
object on the heap.

Of course you can assign a value type to an Object variable in which case a
copy of the value is placed on the heap, and a reference to this new object
is placed in the variable. This is known as Boxing.

Generally use a Class unless you explicitly know you need to make a
Structure! (that was helpful ;-) Also, unless you are calling Win32 API's
with P/Invoke Structures will generally have only one or two member fields
(they are value types and physically copied). For example a complex number.

Hope this helps
Jay



Quote:
> What is the main difference between Structure and Class? And when to use
> Structure and when to use Class?



Sat, 14 May 2005 12:51:01 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Structure vs Class - Performance Gain?

2. Memory (Structure vs Class)

3. Structures vs. classes

4. Serializing a Class within a Class (Previously Structures)

5. Class Libraries vs. Classes

6. Performance: Compiled Classes as DLL vs Classes in Application Code

7. Performance: Compiled Classes as DLL vs Classes in Application Code

8. data class vs datacontrol vs data environemt

9. Performance: Compiled Classes as DLL vs Classes in Application Code

10. Structures as class properties

11. Adding a user control property thats a class or structure

12. Classes and Structures

 

 
Powered by phpBB® Forum Software