The C# Type System

Reference Versus Value Types

The type system of the Common Language Runtime is divided into two worlds. Reference types are heap-allocated types and are used for most objects. Value types are either stack-allocated or allocated as part of another object; they are used for the predefined numeric types and other types that “feel like data.”

This two-type system presents a problem for languages such as C#, which wish to present a unified type system where possible. If reference types and value types remained in separate worlds, it wouldn't be possible to write something like the following:

int value = 55; 
double precision = 0.35;
Console.WriteLine("Result: {0}, {1}", value, precision);

For this code to work would require an ...

Get Programming in the .NET Environment now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.