Difference between class, struct and record

What's the difference between class, struct and record? And it looks like you can use them together. How does this work?
5 Replies
cap5lut
cap5lut12mo ago
classes are reference types, meaning that their memory will be allocated on the heap and u get a reference back which u store in variables, fields, etc. records are in the end just syntax sugar for classes, so that for example properties, GetHashCode(), Equals() and ToString() are automatically generated. structs are value types, meaning that the whole memory is the value and will be stored in a variable/field, not a reference to it like with classes. this also means that structs can exist directly on the stack and that if they are passed to some other variable/field/method, a copy is passed. there are also record structs, they do the same as normal records, just for structs
And it looks like you can use them together. How does this work?
this is not entirely true, a class cant be a struct or vice versa, with one exception, that structs can be boxed (meaning stored on the heap) and thus are then "objects"
JuniorBecari10
JuniorBecari10OP12mo ago
The latter question is because I've seen record structs and record classes too, not "struct class", I know, my bad.
cap5lut
cap5lut12mo ago
there is also one type we didnt talk about, ref structs, these are structs that can never end up on the heap (right now they also cant be used as generics) they are special, because it is the only type that can store a ref or a ref readonly these are basically managed pointers for example this would print 5
int[] array = [0, 1, 2, 3];
ref int secondElement = ref array[1];
secondElement = 5;
Console.WriteLine(array[1]);
int[] array = [0, 1, 2, 3];
ref int secondElement = ref array[1];
secondElement = 5;
Console.WriteLine(array[1]);
JuniorBecari10
JuniorBecari10OP12mo ago
Just like C++ references
cap5lut
cap5lut12mo ago
C++, secondElement would be a pointer
public void Mutate(ref int num)
{
num = 5;
}
public void Mutate2(int num)
{
num = 10;
}

int[] array = [0, 1, 2, 3];
Mutate(ref array[1]);
Console.WriteLine(array[1]); // prints 5

Mutate2(array[1]);
Console.WriteLine(array[1]); // still prints 5
public void Mutate(ref int num)
{
num = 5;
}
public void Mutate2(int num)
{
num = 10;
}

int[] array = [0, 1, 2, 3];
Mutate(ref array[1]);
Console.WriteLine(array[1]); // prints 5

Mutate2(array[1]);
Console.WriteLine(array[1]); // still prints 5
in c++ this would be
int[] array = [0, 1, 2, 3]; // dunno how arrays are initialized in c++
int* secondElement = &array[1];
*secondElement = 5;
Console.WriteLine(array[1]);
int[] array = [0, 1, 2, 3]; // dunno how arrays are initialized in c++
int* secondElement = &array[1];
*secondElement = 5;
Console.WriteLine(array[1]);
but in the end, besides method parameters and local variables, only ref structs are allowed to store ref types. the most common are Span<T> and ReadOnlySpan<T> (they basically store a ref T and an int length)
Want results from more Discord servers?
Add your server