Skip to main content
 首页 » 编程设计

c#之如何在 string.format 中显示 ListDayOfWeek> 的所有内容

2024年02月06日12mate10pro

这是一个针对不同餐厅以及它们在一周中的哪几天营业的小型应用程序。在我的界面中,一周中的每一天都有复选框。我想在每个选定的天数(选中)框中输出 string.Format。这可能吗? 这是获取复选框的代码:

foreach (CheckBox cb in grpDaysOpen.Controls)   // Add days of week that the restaurant is open 
{ 
    DayOfWeek enumConvertedSuccessfully;        // Will contain good enums soon 
    if (cb.Checked && Enum.TryParse<DayOfWeek>(cb.Text, out enumConvertedSuccessfully)) 
        favoriteDiningPlace.OpenDays.Add(enumConvertedSuccessfully); 
} 

这是当前覆盖 ToString:

return string.Format( 
    "{0} is a chian of {1} with a seating capacity of {2}:\r\nSmoking is {3}. Last month's sales were {4}, while last months costs were {5}. \r\nThe restaurant is open {6}", 
    this.Name,  
    this.Chain,  
    this.SeatingCapacity,  
    this.Smoking,  
    this.LastMonthSales,  
    this.LastMonthCosts,  
    this.OpenDays); 

我不知道是否需要在其中包含任何其他代码。如果您需要澄清,请告诉我。目前上面的ToString输出

"system.collections.generic.list`[System.DayOfWeek]"

对于 this.OpenDays。

请您参考如下方法:

List<DayOfWeek> d = new List<DayOfWeek> { DayOfWeek.Monday, DayOfWeek.Saturday }; 
string res = string.Join(", ", d);