How can I achieve inheritance (or similar) with structs in C#? I know that an abstract struct isn\'t possible, but I need to achieve something similar.
I need it as
In C#, you can use interfaces to achieve something akin to polymorphism with value types (structs) as you can't derive directly from a struct but you can have multiple struct types implement specific interfaces.
Therefore, instead of your abstract struct, Vertex, you can have an interface, IVertex.
interface IVertex
{
int SizeInBytes { get; }
void SetPointers();
}
However, it is exceedingly rare that you need to implement your own value types, so make sure you really need value type semantics before proceeding. If you do implement your own value types, make sure they're immutable as mutable value types are a gateway to all kinds of horrible problems.
You should be aware that boxing will occur when casting from a value type to an interface. Not only does this have implications if your value types are mutable (don't make mutable value types), but this will decrease, or most likely cancel out any memory advantage you may gain from using a value type, depending on when or how you do this and whether you do it for every value - use a profiler if you're unsure.