liveBook · Manning

Value type

As explained in .NET in Action, Second Edition

A value type is a .NET type whose values contain their data directly.

A value type is a .NET type whose values contain their data directly. Types can also be reference types, which store a reference to their data. This distinction affects how values behave when passed to methods or copied to other variables. section 2.4

Keep these value-type behaviors in mind:

  • Recognize common value types such as int and float, along with structs and enums. section 2.4
  • Treat value types as objects when required through boxing, and recover the value through unboxing. section 2.4
  • Remember that an integer property such as Book.Id cannot be null unless it is explicitly marked nullable. section 5.2.2
  • Use the distinction alongside C#’s strong typing: variables, constants, and value-producing expressions have specific types. section 2.4

Passing an integer to an object parameter

An int named count can be passed to Console.WriteLine even when the relevant parameter expects object. C# automatically boxes count, wrapping the value type as an object. section 2.4

How .NET in Action, Second Edition covers it

How deep it goes
Defines direct data storage, contrasts references, and introduces boxing without detailing copying or parameter-passing semantics.
Examples
Uses an int passed to Console.WriteLine and an integer Book.Id property to illustrate boxing and nullability.
What you can do after
Evaluate whether a .NET type is a value type and recognize when C# boxes it for an object parameter.
How it connects
Connects value types to reference types, object, boxing, unboxing, nullable types, and the Common Type System.

Also explained in

Compare how 2 books explain value type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.