c# deriving from int32

后端 未结 5 1292
滥情空心
滥情空心 2020-12-31 01:38

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

5条回答
  •  一整个雨季
    2020-12-31 01:53

     public static explicit operator RoomId(int value) {
         return new RoomId { Id = value };
     }
    

    You could then do:

     RoomId variable = (RoomId)10;
    

    It's not possible to derive a class from Int32 or any other value type in C#.

提交回复
热议问题