I have a class of this type:
class A<TResult>
{
public TResult foo();
}
But sometimes I need to use this class as a non generic class, ie the type TResult is void.
I can’t instantiate the class in the following way:
var a = new A<void>();
Also, I’d rather not specify the type omitting the angle brackets:
var a = new A();
I don’t want re-write the whole class because it does the same thing.
The
voidisn’t a real type in C#, even there is a correspondingSystem.Voidstruct in FCL. I’m afraid you need a non-generic version here like this:you can see in FCL there are
System.Action/System.Action<T>, instead ofSystem.Action<void>, as well asTaskinstead ofTask<void>.EDIT From CLI specification(ECMA-335):