Extension Methods
Tim presented extension methods and used as an example several date extension methods, One of them was DaysUntil:
///
/// Gets the number of days until the specified date.
///
///
///
public static double DaysUntil(this DateTime dt)
{
DateTime nextDay;
if (dt > DateTime.Now)
nextDay = new DateTime(dt.Year, dt.Month, dt.Day);
else
nextDay = new DateTime(dt.Year + 1, dt.Month, dt.Day);
return nextDay.Subtract (DateTime.Now).TotalDays;
}
Usage:
var dt = new DateTime(DateTime.Now.Year, cmbMonth.SelectedIndex + 1, cmbDay.SelectedIndex + 1);
var du = dt.DaysUntil();
He indicated some disadvantages, chief among them being the inability to reflect upon them. There is no way of knowing via reflection that DaysUntil is an extension method. Interestingly if you write an extension method which shares the same functionality of an existing method the extension method will not get called. This means you could write extension methods for future functionality so that when recompiled for a later framework those methods would run natively without the need to delete them. Of course you could also wrap them in compiler directives or simply delete them so that also got filed under a disadvantage.
He hinted at some practical extension methods for Enum such as Has, Add, Remove but did not share the source code. I've always been frustrated by the inability to easily detect if an Enum has a certain value (assuming it is decorated with a FlagAttribute. With this extension you could test via myEnum.Has(MyEnum.SomeValue).