Sunday 20 October 2013

C# interview question - value and reference types

Value types

Value types store data. These are allocated on the stack. E.g. int, bool, structs, enums, float, decimal
Declaration is enough to allocate space for value type.
More reading:
  1. Value Types documentation

Reference types

Conceptually same as pointers. They store reference to actual data and not the data. E.g. String, Class types such System.IO.StringReader, user defined classes, etc.
Important to note that all arrays are references even if defined type of array is of value type. Using new keyword is essential to initialize and allocate space for reference types.
More reading:
  1. Reference Types documentation
More interesting reading:
HTH.

No comments:

Post a Comment