11. Structs

Structs are similar to classes in that they represent data structures that can contain data members and function members. However, unlike classes, structs are value types and do not require heap allocation. A variable of a struct type directly contains the data of the struct, whereas a variable of a class type contains a reference to the data, the latter known as an object.

Eric Lippert

The statement that “structs do not require heap allocation” is not the statement “all instances of all structs are always allocated on the stack.” First, the second statement is not true: The memory for a DateTime field of a Customer class will be allocated on the heap along with the rest of the memory of the Customer class. Second, whether a local ...

Get The C# Programming Language (Covering C# 4.0), Fourth Edition 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.