Invert the object or mark it with a negative minus sign

Is it possible to invert an object or mark it as negative?

This is what I want to achieve:

DateTime.Now.Add(-TimeUnits.Week)

This should return the current date time minus 7 days.

Therefore, I need to detect in the method Addif the passed object is negative or not!

I looked at Struct, but my experience with them is too little to say whether this is possible or not! I currently have an enumeration and, of course, it is very limited. I am not attached to an enumeration, so it could be any other object!

My listing TimeUnits:

public enum TimeUnits
{
    Once = 0,
    Day = 1,
    Week = 2,
    Month = 3,
    Quarter = 4,
    Year = 5
}

I have an extension method on DateTimewhere I want to pass an object to a method Addas follows:

private static DateTime? Add(this DateTime current, TimeUnits unitOfTime)
{
    switch (unitOfTime)
    {
        case TimeUnits.Once:
            return null;
        case TimeUnits.Day:
            return current.AddDays(1);
        case TimeUnits.Week:
            return current.AddDays(7);
        case TimeUnits.Month:
            return current.AddMonths(1);
        case TimeUnits.Quarter:
            return current.AddMonths(3);
        case TimeUnits.Year:
            return current.AddYears(1);
        default:
            throw new ArgumentOutOfRangeException("unitOfTime");
    }
}
+4
2

- , , , . : -((int(TimeUnit.Week) - -2 , .

"" ( int ), . , . - , , , , Subtract().

+2

:

private static DateTime? Add(this DateTime current, int units, TimeUnits unitOfTime)

units - unitOfTime current.


:

DateTime.Now.Add(  1, TimeUnits.Week); // add a week
DateTime.Now.Add( 42, TimeUnits.Week); // add multiple weeks
DateTime.Now.Add(- 1, TimeUnits.Week); // subtract a week
DateTime.Now.Add(-42, TimeUnits.Week); // subtract multiple weeks
DateTime.Now.Add(  0, TimeUnits.Week); // return `current`
//etc...

: double units, NaN.

0

All Articles