Struct

Class

Definition: A Struct is a value type if it holds the data within its own memory allocation. A Class is reference type -contains a pointer to another memory location that holds the data.
Includes:
  • All numeric data types

  • Boolean , Char, and Date

  • All structures, even if their members are reference types

  • Enumerations, since their underlying type is always SByte, Short, Integer, Long, Byte, UShort, UInteger, or ULong

  • String

  • All arrays, even if their elements are value types

  • Class types, such as Form

  • Delegates

Holds Actual Data: Yes No
Pointer to another memory location: No Yes
Example: Char, Integer Class, Object, String
Memory Allocation: On stack or in-line in structure On heap
Data Storage: Structs directly store their data in the struct Classes store a reference to a dynamically allocated object
When to use: Structs are useful for small data structures Classes are useful for large data structures
Peformance: Structs can affect performance Performance of class is very good
Constructor Memory Allocation: A struct constructor simply returns the struct value itself (copy), and this value is then copied as necessary Class constructors are invoked with the new operator, but that does not allocate memory on the heap
Multiple References: With structs, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other. Value of each veriable might have different value. With classes, multiple variables may have a reference to the same object
Inheritance: Structs do not support user-specified inheritance, and they implicitly inherit from type object Class supports inheritance