是否有更好的简单 if 语法,分配变量

发布于 2024-12-27 05:25:13 字数 466 浏览 1 评论 0原文

我的代码基本上在一种情况下分配一个变量,在另一种情况下分配不同的变量。有没有更简洁、更高效的方法?

即,不占用那么多线路。或者这是最好的方法?

 if (ViewBag.Date != RoomBooking.StartDateTime.Date || ViewBag.DayPlannerStartTime * 12 > (Int32)RoomBooking.StartDateTime.TimeOfDay.TotalMinutes / 5)
        {
            StartBlock = ViewBag.DayPlannerStartTime * 12;
        }
        else
        {
            StartBlock = ((Int32)RoomBooking.StartDateTime.TimeOfDay.TotalMinutes / 5);
        }

I have code that basically assigns a variable in one case, else a different in another. Is there a neater way that is more efficient?

I.e., not taking up so many lines. Or is this the best way?

 if (ViewBag.Date != RoomBooking.StartDateTime.Date || ViewBag.DayPlannerStartTime * 12 > (Int32)RoomBooking.StartDateTime.TimeOfDay.TotalMinutes / 5)
        {
            StartBlock = ViewBag.DayPlannerStartTime * 12;
        }
        else
        {
            StartBlock = ((Int32)RoomBooking.StartDateTime.TimeOfDay.TotalMinutes / 5);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

忆依然 2025-01-03 05:25:13

也许是这样的(可以说它更整洁,但绝对具有相同的效率):

StartBlock = (ViewBag.Date != RoomBooking.StartDateTime.Date || ViewBag.DayPlannerStartTime * 12 > (Int32)RoomBooking.StartDateTime.TimeOfDay.TotalMinutes / 5)
    ? ViewBag.DayPlannerStartTime * 12
    : ((Int32)RoomBooking.StartDateTime.TimeOfDay.TotalMinutes / 5);

编辑:您也可以稍微优化条件。我怀疑您的 DayPlannerStartTime 以秒为单位,如果我是对的,您可以按以下方式重写比较(我只是将 > 运算符的两个操作数除以12,TotalMinutes 除以 5*12 就变成了 TotalHours):

ViewBag.DayPlannerStartTime > (Int32)RoomBooking.StartDateTime.TimeOfDay.TotalHours

Maybe this (it's arguably neater, but definitely has the same efficiency):

StartBlock = (ViewBag.Date != RoomBooking.StartDateTime.Date || ViewBag.DayPlannerStartTime * 12 > (Int32)RoomBooking.StartDateTime.TimeOfDay.TotalMinutes / 5)
    ? ViewBag.DayPlannerStartTime * 12
    : ((Int32)RoomBooking.StartDateTime.TimeOfDay.TotalMinutes / 5);

EDIT: You can slightly optimize the condition as well. I suspect that your DayPlannerStartTime is expressed in seconds, and if I'm right you can rewrite the comparison the following way (I just divided both operands of the > operator by 12, and TotalMinutes divided by 5*12 became TotalHours):

ViewBag.DayPlannerStartTime > (Int32)RoomBooking.StartDateTime.TimeOfDay.TotalHours
白昼 2025-01-03 05:25:13

是的,您可以使用 ?: 运算符 创建单个表达式,如果条件为 true,则计算结果为第一个表达式,否则计算为第二个表达式:

// condition ? first_expression : second_expression;
var value = (something that is true or false) ? value if true : value if false;

Yes, you can use the ?: operator to create a single expression that will evaluate to the first expression if the condition is true, or else to the second expression:

// condition ? first_expression : second_expression;
var value = (something that is true or false) ? value if true : value if false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文