According to wikipedia a data structure is a data organization, management and storage format that enables efficient access and modification. Data structures are based on the computers ability to fetch and store data in the memory, by a pointer representing a memory address.
Keywords vs CLR:
All components in .NET derive from System.Object. This means that everything in .NET is an object and therefore a class. Is a struct a class? Let’s test this using a decompiler.
public class MyClass { } public struct MyStryct { } public enum MyEnum { } class Program { static void Main(string[] args) { long value = 20; } }
Let’s see what the CLR has to say about this.
A class in IL is a class:
.class public auto ansi beforefieldinit StructVsClass.MyClass extends [mscorlib]System.Object { .method public hidebysig specialname rtspecialname instance void .ctor () cil managed { IL_0000: ldarg.0 IL_0001: call instance void [mscorlib]System.Object::.ctor() IL_0006: nop IL_0007: ret } }
A struct in IL extends System.ValueType which is a class:
.class public sequential ansi sealed beforefieldinit StructVsClass.MyStryct extends [mscorlib]System.ValueType { .pack 0 .size 1 }
An enum in IL extends System.Enum which is a class:
.class public auto ansi sealed StructVsClass.MyEnum extends [mscorlib]System.Enum { }
Our numerical value is of type INT64 which is a class:
.method private hidebysig static void Main ( string[] args ) cil managed { .entrypoint .locals init ( [0] int64 'value' ) IL_0000: nop IL_0001: ldc.i4.s 20 IL_0003: conv.i8 IL_0004: stloc.0 IL_0005: ret } //from mscorlib: .class public sequential ansi sealed serializable beforefieldinit System.Int64 extends System.ValueType implements System.IComparable, System.IFormattable, System.IConvertible, class System.IComparable`1, class System.IEquatable`1 { //much code }
In the end, they are all different kinds of data types which translates to classes. Each type of class will determine how will the memory be managed for that kind of data type.
According to microsoft:
The first difference between reference types and value types we will consider is that reference types are allocated on the heap and garbage-collected, whereas value types are allocated either on the stack or inline in containing types and deallocated when the stack unwinds or when their containing type gets deallocated. Therefore, allocations and deallocations of value types are in general cheaper than allocations and deallocations of reference types.
One important difference is that a struct will directly hold it’s member values (except when it has a member that is of reference type for which it will store it’s address) and a class will always hold a reference to it’s members values.
To exemplify this I’ve made a quick example. A class and a struct each containing a primitive value member. For the struct you can see that the struct address has the same value as the address of it’s member (value) while for the class it’s not the same thing.
static void Main(string[] args) { var myClass = new MyClass(); var myStruct = new MyStryct(); unsafe { //class object adress TypedReference clsTR = __makeref(myClass); IntPtr clsPtr = **(IntPtr**)(&clsTR); Console.WriteLine($"Address of class object : 0x{clsPtr.ToString("x")}"); //class field adress fixed (int *clsvalPtr = &(myClass.Value)) { Console.WriteLine($"Address of class field : 0x{((int)clsvalPtr).ToString("x")}"); Console.WriteLine($"Class and it's field address equality :{(int)clsPtr == (int)clsvalPtr} "); } //struct object address MyStryct* strPtr = &myStruct; Console.WriteLine($"Address of struct object : 0x{((int)strPtr).ToString("x")}"); //struct field address int* strValPtr = &myStruct.Value; Console.WriteLine($"Address of struct field : 0x{((int)strValPtr).ToString("x")}"); Console.WriteLine($"Struct and it's field address equality :{(strPtr == strValPtr)} "); } }

