VB.NET Docs

Classes and Structures in Visual Basic

Understanding Types

VB.NET provides two primary ways to define complex data structures: classes (reference types) and structures (value types). Choose a class when you need inheritance, polymorphism, or large immutable objects. Use a structure for small, immutable data that benefits from stack allocation.

Show Example ►

Key Differences

AspectClassStructure
Memory AllocationHeap (reference type)Stack (value type)
InheritanceSupports inheritanceNo inheritance (except interfaces)
Default ConstructorCan define none; compiler provides oneMust have parameterless constructor automatically
AssignmentCopies referenceCopies entire value
NullabilityCan be NothingCannot be Nothing (except Nullable(Of T))

When to Use a Structure

When to Use a Class

Best Practices

  1. Prefer Class for most scenarios.
  2. Make structures immutable: declare ReadOnly fields and provide a constructor.
  3. Implement IEquatable(Of T) for structures to improve equality checks.
  4. Avoid exposing mutable reference fields in structures.