What if T is void in Generics? How to omit angle brackets

c#, generics, void

Solution

The `void` isn't a real type in C#, even there is a corresponding `System.Void` struct in FCL. I'm afraid you need a non-generic version here like this:

class A
{
   //non generic implementation
}

class A<T> : A
{
   //generic implementation 
}

you can see in FCL there are `System.Action`/`System.Action<T>`, instead of `System.Action<void>`, as well as `Task` instead of `Task<void>`.

EDIT From CLI specification(ECMA-335):

The following kinds of type cannot be used as arguments in instantiations (of generic types or methods):

Byref types (e.g., System.Generic.Collection.List`1<string&> is invalid)

Value types that contain fields that can point into the CIL evaluation stack (e.g.,List<System.RuntimeArgumentHandle>)

void (e.g.,List<System.Void> is invalid)

Problem

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.

Original source