-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
C# 7.1 and .NET Core 2.0 - Modern Cross-Platform Development - Third Edition
By :
There are two categories of memory: stack memory and heap memory. Stack memory is fast but limited, and heap memory is slow but plentiful.
There are two C# keywords that you can use to create object types: class and struct. Both can have the same members. The difference between the two is how memory is allocated.
When you define a type using class, you are defining a reference type. This means that the memory for the object itself is allocated on the heap, and only the memory address of the object (and a little overhead) is stored on the stack.
When you define a type using struct, you are defining a value type. This means that the memory for the object itself is allocated on the stack.
If a struct uses types that are not of the struct type for any of its fields, then those fields will be stored on the heap!
These are the most common struct types in .NET Core:
byte, sbyte, short, ushort, int, uint, long, ulong, float, double, and decimal...