位标志 - 我错过了什么?
一个简单的代码:
protected void Page_Load(object sender, EventArgs e)
{
DateTime now = DateTime.UtcNow;
lit.Text += "<br/>now.DayOfWeek: " + now.DayOfWeek.ToString();
// weekdays (Saturday is not included)
DayOfWeek runningDays = DayOfWeek.Monday | DayOfWeek.Tuesday | DayOfWeek.Wednesday | DayOfWeek.Thursday | DayOfWeek.Friday;
lit.Text += "<br/>" + runningDays.HasFlag(now.DayOfWeek);
lit.Text += "<br/>" + runningDays.HasAny(now.DayOfWeek);
lit.Text += "<br/>" + ((runningDays & now.DayOfWeek) != 0);
// weekend (Saturday is in a weekend)
runningDays = DayOfWeek.Saturday | DayOfWeek.Sunday;
lit.Text += "<br/>" + runningDays.HasFlag(now.DayOfWeek);
lit.Text += "<br/>" + runningDays.HasAny(now.DayOfWeek);
lit.Text += "<br/>" + ((runningDays & now.DayOfWeek) != 0);
}
助手:
public static bool HasExactly(this DayOfWeek x, DayOfWeek y) { return x == y; }
public static bool HasAny(this DayOfWeek x, DayOfWeek y) { return 0 != (x & y); }
public static bool HasAll(this DayOfWeek x, DayOfWeek y) { return y == (x & y); }
今天的输出(星期六)
now.DayOfWeek: Saturday
True
True
True
True
True
True
但输出应该是这样的:
now.DayOfWeek: Saturday
False
False
False
True
True
True
我在这里缺少什么?
a simple code:
protected void Page_Load(object sender, EventArgs e)
{
DateTime now = DateTime.UtcNow;
lit.Text += "<br/>now.DayOfWeek: " + now.DayOfWeek.ToString();
// weekdays (Saturday is not included)
DayOfWeek runningDays = DayOfWeek.Monday | DayOfWeek.Tuesday | DayOfWeek.Wednesday | DayOfWeek.Thursday | DayOfWeek.Friday;
lit.Text += "<br/>" + runningDays.HasFlag(now.DayOfWeek);
lit.Text += "<br/>" + runningDays.HasAny(now.DayOfWeek);
lit.Text += "<br/>" + ((runningDays & now.DayOfWeek) != 0);
// weekend (Saturday is in a weekend)
runningDays = DayOfWeek.Saturday | DayOfWeek.Sunday;
lit.Text += "<br/>" + runningDays.HasFlag(now.DayOfWeek);
lit.Text += "<br/>" + runningDays.HasAny(now.DayOfWeek);
lit.Text += "<br/>" + ((runningDays & now.DayOfWeek) != 0);
}
A helper:
public static bool HasExactly(this DayOfWeek x, DayOfWeek y) { return x == y; }
public static bool HasAny(this DayOfWeek x, DayOfWeek y) { return 0 != (x & y); }
public static bool HasAll(this DayOfWeek x, DayOfWeek y) { return y == (x & y); }
today's output (Saturday)
now.DayOfWeek: Saturday
True
True
True
True
True
True
But the output should be like:
now.DayOfWeek: Saturday
False
False
False
True
True
True
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
星期几不是位标志。 http://msdn.microsoft.com/en-us/library /system.datetime.dayofweek.aspx
Days of week are not bit flags. http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek.aspx
我知道这是一篇旧文章,但以防万一 - 进行标志枚举的简洁语法是
您需要 None 来指示没有选择任何内容。这样的枚举允许您指示多天,例如:
希望不需要注释。为了“解码”该值:
然后这允许我们调用:
希望这对某人有帮助。
I know this is an old post but just in case - a neat syntax to do flags enums is
You would need None to indicate nothing was selected. Such an enum allows you to indicate multiple days, for example:
No comment needed hopefully. And in order to "decode" the value:
And this then allows us to call:
Hope this helps someone.
如果您需要像标志一样使用它,您可以创建自己的 DayOfWeek 枚举:
You could create your own DayOfWeek enum if you need to use it like flags:
DayOfWeek 有连续的数字。
使用
List
。转到定义显示:
DayOfWeek has sequential numbers.
Use a
List<DayOfWeek>
.Go to definition shows:
DayOfWeek
枚举旨在表示一周中的某一天,而不是一组天。使用
&
不会出现编译器错误,因为枚举是整数,但运算符不会执行您期望的操作(除非您恰好只在星期一、星期二或星期四安排事件) 。如果你想要一个位标志,那么声明一个位标志类型,就像 Ray 建议的那样。
The
DayOfWeek
enumeration was designed to represent a single day of the week, not a set of days.You don't get a compiler error for using
&
because enums are integers, but the operator doesn't do what you expect (unless you happen to only schedule events on Mondays, Tuesdays, or Thursdays).If you want a bit flag, then declare a bit flag type, like Ray suggested.