Managing memory with reference and value types

There are two categories of memory: stack memory and heap memory. Stack memory is fast but limited and heap memory is slower but plentiful.

There are two C# keywords that you use to create object types: class and struct. Both can have the same members. The difference between the two is how memory is allocated and the lifetime of their variables.

Note

Technically, how memory is allocated is an implementation detail and not part of the C# or .NET specifications. Alternative implementations could choose to store instances of a struct type on the heap! You can read more about how value types are stored here: https://blogs.msdn.microsoft.com/ericlippert/2010/09/30/the-truth-about-value-types/.

When you define ...

Get C# 6 and .NET Core 1.0: Modern Cross-Platform Development 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.