gpt4 book ai didi

c# - 查找谓词 DayOfWeek Issue c#

转载 作者:太空宇宙 更新时间:2023-11-03 20:56:31 25 4
gpt4 key购买 nike

最近我遇到了一个问题,当我试图在 DayOfWeek 的列表中找到特定的一天时,它在星期日效果不佳,但在其他所有日子都有效。

我做了一个谓词,但是当我在一个空列表或一个不包含星期日的列表中寻找星期日时,它找到了它。

例子:

namespace testing1
{
public partial class Timer : Form
{
public static List<dayOfWeek> day = new List<dayOfWeek>();//I can set things in it

public Timer()
{
InitializeComponent();
}

private void Timer_Load(object sender, EventArgs e)
{
Console.WriteLine(day.Count); //return 0 when I instantiate the form without inserting from an another form.

checkBox1.Checked = day.Find((dayOfWeek d) => { return d == dayOfWeek.Monday; }) == dayOfWeek.Monday; // return false OK
checkBox2.Checked = day.Find((dayOfWeek d) => { return d == dayOfWeek.Tuesday; }) == dayOfWeek.Tuesday; // return false OK
checkBox3.Checked = day.Find((dayOfWeek d) => { return d == dayOfWeek.Wednesday; }) == dayOfWeek.Wednesday; // return false OK
checkBox4.Checked = day.Find((dayOfWeek d) => { return d == dayOfWeek.Thursday; }) == dayOfWeek.Thursday; // return false OK
checkBox5.Checked = day.Find((dayOfWeek d) => { return d == dayOfWeek.Friday; }) == dayOfWeek.Friday; // return false OK
checkBox6.Checked = day.Find((dayOfWeek d) => { return d == dayOfWeek.Saturday; }) == dayOfWeek.Saturday; // return false OK
checkBox7.Checked = day.Find((dayOfWeek d) => { return d == dayOfWeek.Sunday; }) == dayOfWeek.Sunday; // return true WTF ????
}
}
}

需要我帮忙吗?

最佳答案

DayOfWeek是一个 enum因此它是值类型,值类型变量不能有空值,除非标记为Nullable<> ,这意味着很可能是 DayOfWeek 中的第一个值enumSunday 这将是默认值,所以无论何时 Find无法找到该值,它在本例中返回默认值 星期日

您应该改用 Contains :

day.Contains(DayOfWeek.Sunday);
//..

关于c# - 查找谓词 DayOfWeek Issue c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50003661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com