Explain the difference between value types and reference types in C#.

Beginner

Answer

Value Types:

  • Stored directly in memory (stack for local variables)
  • Hold the actual data
  • Examples: int, bool, char, struct, enum
  • Assignment copies the value
  • Default values are zero/false/null equivalent
    Reference Types:
  • Stored on the heap, variable holds a reference to the memory location
  • Examples: class, string, array, delegate
  • Assignment copies the reference, not the data
  • Default value is null
  • Multiple variables can reference the same object