Let’s say I want to get extra type-checking for working with primitives that mean different things semantically:
public struct Apple
{
readonly int value;
// Add constructor + operator overloads
}
public struct Orange
{
readonly int value;
// Add constructor + operator overloads
}
The point is we can’t compare “apples to oranges”, so wrapping up the actual int in a struct means we get type checking and some extra readability and documentation by code.
My question is: what is the overhead associated with doing this, both in terms of memory and speed? Since structs are value types, would variables containing these structs be 32 bits or larger? What about performance overhead of using these structs instead of primitives – is there a large overhead for the operator overloads?
Any other advice on the wisdom of doing this?
In memory there is no overhead using a struct which you can verify using
Marshal.SizeOf():This also is the same as returned by
sizeof(testStruct):According to MSDN the difference between the two sizing methods is: