public static class Extension
{
public static void Test(this DateTime? dt)
{
}
}
void Main()
{
var now = DateTime.Now;
Extension.Test(now); //
var isn't a type. The actual type is figured out at compile-time. If you set DateTime.Now for a var, it will recognize as a DateTime type, not a Nullable, and that's why it doesn't compile.
var variables are also known as Implicitly Typed Local Variables (C# Programming Guide)
By the way, you can also create a generic extension method for nullable types:
public static T? GenericMethod(this T? source) where T : struct
{
//Do something
}
and you can call whatever is nullable, declaring its type:
DateTime? dateTimeNullable = DateTime.Now;
dateTimeNullable.GenericMethod();
int? intNullable = 0;
intNullable.GenericMethod();