I have an extension method:
public static int DoStuff(this MyEnum? enumValue)
{
...
}
I want to invoke it on a value of MyEnum
which is known to be not null:
MyEnum enumVal = ...
var x = enumVal.DoStuff(); // compiler error!
Error:
error CS1928: 'MyEnum' does not contain a definition for 'DoStuff' and the best extension method overload 'MyExtensions.DoStuff(MyEnum?)' has some invalid arguments
I can work around it by declaring an overload of the extension:
public static int DoStuff(this MyEnum enumValue)
{
return DoStuff((MyEnum?)enumValue);
}
Alternatively, I can invoke the extension class explicitly:
var x = MyExtensions.DoStuff(enumVal);
But this is non-intuitive. Why can't an extension of a Nullable<T>
accept a value of type T
, just like any normal method signature can?