i have several classes with members called ‘Id’. Originally i wanted to store these as ints, but i would like some layer of protection, to make sure i don’t accidentally assign a room id to a person etc.
one solution would be typedef (using RoomId = System.Int32;) but then i need that line of code in all files using these. i would prefer e.g. a RoomId class derived from int32, but i can’t figure out how to set it up to allow explicit conversion (for initilisation)
or should i do this in some other way?
If I understand correctly, the only operation you really need is comparison for equality. You can create a RoomId class (or struct, whichever suits you)
You can implement IEquatable, overload the equality and inequality operators if you want. If you need persistence, you could implement the ISerializable interface to make sure that the integer value only “escapes” the class if it is really needed.