I have a program that is interfacing with an external library that, among other things, has an unsigned 12-bit value packed in a larger struct.
This used to be 8 bits, so we simply marshaled it as a byte.
Now that it’s 12 bits… I can use a ushort, but that opens up issues of (a) range checking and (b) marshaling.
Is there a simple way of implementing a constrained numeric type like this, where I don’t have to override every assignment and comparison method?
You should create a struct that overrides the implicit conversion operator: