Slide in HTML version: https://bit.ly/2Ui4dC2
C# Types Overview
Value Type and Reference Type
Structure type and Enumeration type (
Common Questions & Answers
This tutorial will cover most of the terms in the diagram, but we are not going to dive deep into dynamic type.
The fundamental difference is how they are handled in memory.
Stack is used for static memory allocation, it happens at compile time.
Heap is used for dynamic memory allocation, it happens at runtime.
Compile time is when you build your project.
Runtime is when you execute your project.
Value types are stored directly on the stack during compile time.
Reference types have an object reference variable created on the stack during compile time. At runtime, the actual custom object of the Reference type will be created on the Heap.
Note: This also means the reference types have some overhead in terms of memory consumption, each reference to an object requires an extra four or eight bytes, depending on whether the .NET runtime is running on a 32- or 64-bit platform.
Value types are destroyed immediately after the scope is lost.
For Reference types, only the reference variable on the stack is destroyed after the scope is lost. The actual object on the heap is later destroyed by garbage collector.
When you copy a value type , a new copy of that variable gets created and modifications on the original variable will not affect the values contained by the copied variable.
When you copy a reference type, we only get a copy of the reference variable. Both the reference variables points to the same object on the heap. So, operations on one variable will affect the values contained by the other reference variable.
Reference type can be assigned to null, which means the reference points to no object, and trying to access the fields of a null object will cause run-time error (NullReferenceException).
Value types cannot have a null value, it will cause compile-time error.
Nuallable Reference type and nullable value types will be discussed in the next section.
Arrays (even of value types like int) are reference types in C#.
In C#, arrays are actually objects.
System. Array is the abstract base type of all array types.
The nullable value types are available beginning with C# 2.
C# 8.0 has introduced nullable reference types, C# also has a construct called nullable value types for representing value-type nulls.
Avoid null reference mistakes at compile-time.
A way to explicitly express our intent.
Nullable Reference Type is by default enabled in .NET Core 3.0 projects.
Structure is a group of basic data type variables, it is similar to class (a data type in OOP).
Enumeration is a data structure that assigns integer values to string type data values.
struct and enum types are value types stored on the stack, if used properly, they can be more efficient than class.
enums are basically a set of named constants. They are declared in C# using the
enum keyword. Every
enum type automatically derives from
struct is a weakened version of class.
Example of structs in the .NET Framework:
If instances of the type are small and commonly short-lived or are commonly embedded in other objects.
Define a struct when the type has all of the following characteristics:
C# Types (Value Type and Reference Type)
Stack and Heap
Struct and Enum in Value Type
Difference between struct and class
Thanks for listening / reading :)