O'Reilly logo

The C# Programming Language (Covering C# 4.0), Fourth Edition by Peter Golde, Scott Wiltamuth, Mads Torgersen, Anders Hejlsberg

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required