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
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"
The latter question is because I've seen record structs and record classes too, not "struct class", I know, my bad.
there is also one type we didnt talk about,
ref struct
s, 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
Just like C++ references
C++,
secondElement
would be a pointer
in c++ this would be
but in the end, besides method parameters and local variables, only ref struct
s 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
)