Variables, Parameters, and Fields

Reference types always yield instances that are allocated on the heap. In contrast, value types yield instances that are allocated relevant to the context in which the variable is declared. If a local variable is of a value type, the CLR allocates the memory for the instance on the stack. If a field in a class is a member of a value type, then the CLR allocates the memory for the instance as part of the layout of the object or type in which the field is declared. The rules for dealing with value and reference types are consistent for variables, fields, and parameters. To that end, this chapter will use the term variable to refer to all three concepts and will use the term local variable when discussing variables ...

Get Essential .NET, Volume 1: The Common Language Runtime 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.