English 中文(简体)
如果有,分配变量,则有较好的合成物
原标题:Is there better syntax for simple if, assigning variable

我的法典基本上在一种情况下分配变量,而在另一种情况下则有所不同。 是否有更高效的 ne子?

I.e.,不走这么多条线。 或者,这种最好的方式?

 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);
        }
最佳回答

或许可以这样做(这无疑是不可辩驳的,但无疑具有同样的效率):

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

http://www.un.org。 你们也可以稍微优化条件。 我怀疑您的<代码>DayPlannerStarttime的表述是第二版,如果我右边可以改写以下方式(我只将<代码>和>;的操作者按12条分列;< 代码>TotalMinutes>,按5*12分列成为):

ViewBag.DayPlannerStartTime > (Int32)RoomBooking.StartDateTime.TimeOfDay.TotalHours
问题回答

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;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签