位标志 - 我错过了什么?

发布于 2024-09-15 05:45:42 字数 1411 浏览 12 评论 0原文

一个简单的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

俏︾媚 2024-09-22 05:45:42

星期几不是位标志。 http://msdn.microsoft.com/en-us/library /system.datetime.dayofweek.aspx

中常量的值
DayOfWeek 枚举范围为
DayOfWeek.Sunday 至
DayOfWeek.Saturday。如果投射到
整数,其值范围从零开始
(表示 DayOfWeek.Sunday)
六(这表明
DayOfWeek.Saturday)。

Days of week are not bit flags. http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek.aspx

The value of the constants in the
DayOfWeek 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).

回眸一笑 2024-09-22 05:45:42

我知道这是一篇旧文章,但以防万一 - 进行标志枚举的简洁语法是

[Flags]
public enum DaysOfWeek
{
    None = 1 << 0,
    Monday = 1 << 1,
    Tuesday = 1 << 2,
    Wednesday = 1 << 3,
    Thursday = 1 << 4,
    Friday = 1 << 5,
    Saturday = 1 << 6,
    Sunday = 1 << 7
}

您需要 None 来指示没有选择任何内容。这样的枚举允许您指示多天,例如:

var selectedDays = DaysOfWeek.Tuesday | DaysOfWeek.Friday;

希望不需要注释。为了“解码”该值:

public bool IsDayOfWeekSelected(DaysOfWeek which, DaysOfWeek selection)
{
    return selection & which == which;
}

然后这允许我们调用:

var isTuesday = IsDayOfWeekSelected(DaysOfWeek.Tuesday, selectedDays); // => true
var isWednesday = IsDayOfWeekSelected(DaysOfWeek.Wednesday, selectedDays); // => false
var isFriday = IsDayOfWeekSelected(DaysOfWeek.Friday, selectedDays); // => true

希望这对某人有帮助。

I know this is an old post but just in case - a neat syntax to do flags enums is

[Flags]
public enum DaysOfWeek
{
    None = 1 << 0,
    Monday = 1 << 1,
    Tuesday = 1 << 2,
    Wednesday = 1 << 3,
    Thursday = 1 << 4,
    Friday = 1 << 5,
    Saturday = 1 << 6,
    Sunday = 1 << 7
}

You would need None to indicate nothing was selected. Such an enum allows you to indicate multiple days, for example:

var selectedDays = DaysOfWeek.Tuesday | DaysOfWeek.Friday;

No comment needed hopefully. And in order to "decode" the value:

public bool IsDayOfWeekSelected(DaysOfWeek which, DaysOfWeek selection)
{
    return selection & which == which;
}

And this then allows us to call:

var isTuesday = IsDayOfWeekSelected(DaysOfWeek.Tuesday, selectedDays); // => true
var isWednesday = IsDayOfWeekSelected(DaysOfWeek.Wednesday, selectedDays); // => false
var isFriday = IsDayOfWeekSelected(DaysOfWeek.Friday, selectedDays); // => true

Hope this helps someone.

謸气贵蔟 2024-09-22 05:45:42

如果您需要像标志一样使用它,您可以创建自己的 DayOfWeek 枚举:

[Flags]
public enum MyDayOfWeek { Sunday = 1, Monday = 2, Tuesday = 4, ... , Saturday = 64 };

You could create your own DayOfWeek enum if you need to use it like flags:

[Flags]
public enum MyDayOfWeek { Sunday = 1, Monday = 2, Tuesday = 4, ... , Saturday = 64 };
简美 2024-09-22 05:45:42

DayOfWeek 有连续的数字。

使用List

转到定义显示:

// Summary:
//     Specifies the day of the week.
[Serializable]
[ComVisible(true)]
public enum DayOfWeek
{
    // Summary:
    //     Indicates Sunday.
    Sunday = 0,
    //
    // Summary:
    //     Indicates Monday.
    Monday = 1,
    //
    // Summary:
    //     Indicates Tuesday.
    Tuesday = 2,
    //
    // Summary:
    //     Indicates Wednesday.
    Wednesday = 3,
    //
    // Summary:
    //     Indicates Thursday.
    Thursday = 4,
    //
    // Summary:
    //     Indicates Friday.
    Friday = 5,
    //
    // Summary:
    //     Indicates Saturday.
    Saturday = 6,
}

DayOfWeek has sequential numbers.

Use a List<DayOfWeek>.

Go to definition shows:

// Summary:
//     Specifies the day of the week.
[Serializable]
[ComVisible(true)]
public enum DayOfWeek
{
    // Summary:
    //     Indicates Sunday.
    Sunday = 0,
    //
    // Summary:
    //     Indicates Monday.
    Monday = 1,
    //
    // Summary:
    //     Indicates Tuesday.
    Tuesday = 2,
    //
    // Summary:
    //     Indicates Wednesday.
    Wednesday = 3,
    //
    // Summary:
    //     Indicates Thursday.
    Thursday = 4,
    //
    // Summary:
    //     Indicates Friday.
    Friday = 5,
    //
    // Summary:
    //     Indicates Saturday.
    Saturday = 6,
}
若沐 2024-09-22 05:45:42

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文