public static class Extension
{
public static void Test(this DateTime? dt)
{
}
}
void Main()
{
var now = DateTime.Now;
Extension.Test(now); //
A DateTime
is not convertible to Nullable
explicitly.
The C# specification, 7.6.5.2 Extension method invocations:
An extension method is eligible if:
...
If no candidate set is found in any enclosing namespace declaration or compilation unit, a compile-time error occurs.
So you have to cast the DateTime
to Nullable
explicitly or use a nullable from the beginning:
DateTime now = DateTime.Now;
((DateTime?)now).Test();
or
DateTime? now = DateTime.Now;
now.Test();