假设,为了这个例子,我试图解析一个文件,该文件指定记录中的两个任意字节代表一周中的哪一天,因此:
DayOfWeek:
- 0 = Monday
- 1 = Tuesday
- 2 = Wednesday
- 3 = Thursday
- 4 = Friday
- 5 = Saturday
- 6 = Sunday
- 7-15 = Reserved for Future Use
我可以定义一个枚举来映射到这个字段,因此:
public enum DaysOfWeek
{
Monday = 0,
Tuesday = 1,
Wednesday = 2,
Thursday = 3,
Friday = 4,
Saturday = 5,
Sunday = 6
ReservedForFutureUse
}
但是我如何定义有效值ReservedForFutureUse
呢?理想情况下,我想做类似的事情:
public enum DaysOfWeek
{
Monday = 0,
Tuesday = 1,
Wednesday = 2,
Thursday = 3,
Friday = 4,
Saturday = 5,
Sunday = 6
ReservedForFutureUse = {7,8,9,10,11,12,13,14,15}
}
这个问题只会随着更复杂的领域而加剧;例如,假设在这种情况下,7 和 8 都映射到相同的错误情况或其他东西。如何在 C# 枚举中捕获这一要求?