## Sunday, August 22, 2010

### Extension method: IEnumerable<DateTime?>.AreChronological()

In this post you can find an extension method which extends IEnumerable<DateTime?>. The AreChronological extension method tests if the items in the IEnumerable<DateTime?> are in chronological order.

There are multiple ways you can solve this problem.

Imperative solution

`   1:  public static class DateTimeExtensions`
`   2:  {     `
`   3:      public static bool AreChronological(this IEnumerable<DateTime?> dateTimes)`
`   4:      {`
`   5:          var prev = (DateTime?)DateTime.MinValue;`
`   6:   `
`   7:          foreach (var dateTime in dateTimes)`
`   8:          {`
`   9:              if (dateTime != null)`
`  10:              {`
`  11:                  if (prev > dateTime)`
`  12:                  {`
`  13:                      return false;`
`  14:                  }`
`  15:                  prev = dateTime;`
`  16:              }`
`  17:          }`
`  18:   `
`  19:          return true;`
`  20:      }`
`  21:  }`

Functional solution

After I finished the imperative solution, I asked Bart De Smet if he knew a sexier solution. He completely Linqified the solution and came up with this.

`   1:  public static bool AreChronological(this IEnumerable<DateTime?> dateTimes)`
`   2:  {`
`   3:      return dateTimes.Where(d => d != null)`
`   4:                      .Aggregate(new { c = (DateTime?)DateTime.MinValue, b = true },`
`   5:                                  (a, d) => new { c = d, b = a.b && a.c <= d }).b;`
`   6:  }`

Usage

You can use the extension method like this.

`   1:  DateTime?[] dateTimesChronological = new DateTime?[] { new DateTime(2010, 5, 15), `
`   2:                                                         null, `
`   3:                                                         new DateTime(2010, 6, 20) }; `
`   4:  DateTime?[] dateTimesNotChronological = new DateTime?[] { new DateTime(2010, 6, 20), `
`   5:                                                            null, `
`   6:                                                            new DateTime(2010, 5, 15) }; `
`   7:   `
`   8:  Console.WriteLine(dateTimesChronological.AreChronological());`
`   9:  Console.WriteLine(dateTimesNotChronological.AreChronological());`

Which solution do you like best? The imperative or the functional solution? Any ideas on how I could improve this code?

### Extension method: DateTime.IsInFuture()

In this post you can find a simple DateTime extension method. The IsInFuture method simply returns a boolean indicating whether the DateTime instance is in the future or not.

`   1:  public static class DateTimeExtensions`
`   2:  {`
`   3:      public static bool IsInFuture(this DateTime dateTime)`
`   4:      {`
`   5:          int compareResult = DateTime.Compare(dateTime, DateTime.Now);`
`   6:   `
`   7:          return compareResult != -1;`
`   8:      }`
`   9:  }`

You can use it like this..

`   1:  DateTime dateTimeInPast = new DateTime(2010, 5, 20);`
`   2:  DateTime dateTimeInFuture = new DateTime(2025, 11, 20);`
`   3:   `
`   4:  Console.WriteLine(dateTimeInPast.IsInFuture()); //Prints False`
`   5:  Console.WriteLine(dateTimeInFuture.IsInFuture()); //Prints True`