c# deriving from int32

后端 未结 5 1347
滥情空心
滥情空心 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 02:09

    You cannot inherit value types (structs, including Int32) in .NET.

    The closest option would be to make a struct which contained nothing but your Int32. This would require the same space, but could be restricted to your exact struct. You could then change your struct later to include other information, if needed. (Be aware, though, that this will probably be a breaking API change.)

提交回复
热议问题