Value Types vs Reference Types
See how each is stored and copied.
Two Families of Types
C# types split into value types (int, double, bool, structs) and reference types (classes, strings, arrays). They differ in how they are stored and copied.
using System;
class Program
{
static void Main()
{
int valueType = 5; // value type
int[] referenceType = { 1, 2, 3 }; // reference type
Console.WriteLine(valueType + ", length " + referenceType.Length);
}
}Value Types Hold the Data
A value-type variable directly contains its data. Typically these live on the stack or inline within their containing object.
using System;
class Program
{
static void Main()
{
int a = 10;
double b = 3.14;
bool c = true;
Console.WriteLine(a + ", " + b + ", " + c);
}
}All lessons in this course
- Value Types vs Reference Types
- Boxing and Unboxing
- Implicit and Explicit Conversions
- The Convert Class and Parsing