Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you assign a value only if its greater / less than the current value?

Tags:

operators

c#

I'm wondering if there's an operator to simplify this? Similar to the += operator.

if (x > val) x = val;
x "new operator" val;


//date times
DateTime d1 = dsi_curr.cycleSteps.StepsUpTimestamp[0] ;
DateTime d2 = dsi_curr.cycleSteps.StepsUpTimestamp[dsi_curr.cycleSteps.StepsUpTimestamp.Length-1];

if (d1 < curbt.details.StartDate) {
    curbt.details.StartDate = d1;
}
if (d2 > curbt.details.EndDate) {
    curbt.details.EndDate = d2;
}
like image 308
Rhett Avatar asked Oct 26 '25 10:10

Rhett


1 Answers

There is no builtin operator for this, but you can add your own method to simplify this:

static void MakeLesserOf(ref DateTime self, DateTime other) {
    self = self > other ? other : self;
}
static void MakeGreaterOf(ref DateTime self, DateTime other) {
    self = self < other ? other : self;
}

Now you can rewrite your code as follows:

MakeLesserOf(curbt.details.StartDate, d1);
MakeGreaterOf(curbt.details.EndDate, d2);
like image 98
Sergey Kalinichenko Avatar answered Oct 28 '25 23:10

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!