1.9. Value and Reference Types

Types in C# are categorized as either value or reference types. The behavior when copying or modifying objects of these types is very different.

An object of a value type stores its associated data directly within itself. Any changes to that data does not affect any other object. For example, the predefined arithmetic types, such as int and double, are value types. When we write

double pi = 3.14159;

the value 3.14159 is directly stored within pi.

When we initialize or assign one value type with another, the data contained in the one is copied to the second. The two objects remain independent.

For example, when we write

double shortPi = pi;

although both pi and shortPi now hold the same value, the values are ...

Get C# Primer: A Practical Approach 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.