Value Types and Reference Types

Structs are value types, so when a struct object is assigned to a variable, the whole value is assigned, as opposed to reference-based values where only the reference is passed along. This can often lead to significantly different behavior between reference and value types.

Note

It is generally more efficient to pass class instances around in a program than struct-based values because the former only involves a simple reference whereas the latter involves the whole value.

Consider, for example, the method called UpdateTime defined in lines 37–42 of Listing 18.4. It has one formal parameter of type TimeSpan. When we ...

Get C# Primer Plus 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.