DayOfWeek的数值是否安全? [英] Is it safe to assume DayOfWeek's numeric value?

查看:124
本文介绍了DayOfWeek的数值是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将星期几存储在数据库中,其中Sunday = 1,Monday = 2等。

I have the day of the week stored in a database, where Sunday = 1, Monday = 2 etc.

在数据库的查询中,我需要将日期转换为 System.DayOfWeek

In a query from the database, I need to convert the day to System.DayOfWeek.

根据 MSDN


此枚举中常量的值范围从 DayOfWeek.Sunday DayOfWeek.Saturday 。如果转换为整数,则其值的范围为零(表示 DayOfWeek.Sunday )至六(表示 DayOfWeek.Saturday

The value of the constants in this enumeration ranges from DayOfWeek.Sunday to DayOfWeek.Saturday. If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday).

这意味着在我查询数据之后,我需要将结果转换为 DayOfWeek 之前减1。

This means that after I query the data, I need to subtract 1 before I convert the result to DayOfWeek.

这是否安全?

或者我需要担心 DayOfWeek的数值改变?

推荐答案

是的,这个安全,你不应该担心价值观的变化。以下是定义枚举的方式:

Yes, this safe, you should not worry about the values changing. Here's how the enum is defined:

public enum DayOfWeek
{
    Friday = 5,
    Monday = 1,
    Saturday = 6,
    Sunday = 0,
    Thursday = 4,
    Tuesday = 2,
    Wednesday = 3
}

如果这些值在BCL中更改,那么很多代码将会中断。

If those values change in the BCL, a hell lot of a code will break.

这篇关于DayOfWeek的数值是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆