What is the equivalent of Java wildcards in C# generics

The normal way to do this would be to make the method generic:

public void ProcessItems<T>(Item<T>[] items) {
  foreach(Item<T> item in items)
    item.DoSomething();
}

Assuming the caller knows the type, type inference should mean that they don't have to explicitly specify it. For example:

Item<int> items = new Item<int>(); // And then populate...
processor.ProcessItems(items);

Having said that, creating a non-generic interface specifying the type-agnostic operations can be useful as well. It will very much depend on your exact use case.


I see that you only want to invoke some method with no parameters... there's already a contract for that: Action.

public void processItems(IEnumerable<Action> actions)
{
  foreach(Action t in actions)
    t();
}

Client:

List<Animal> zoo = GetZoo();
List<Action> thingsToDo = new List<Action>();
//
thingsToDo.AddRange(zoo
  .OfType<Elephant>()
  .Select<Elephant, Action>(e => e.Trumpet));
thingsToDo.AddRange(zoo
  .OfType<Lion>()
  .Select<Lion, Action>(l => l.Roar));
thingsToDo.AddRange(zoo
  .OfType<Monkey>()
  .Select<Monkey, Action>(m => m.ThrowPoo));
//
processItems(thingsToDo);

There's no way you can omit Type Parameters in .NET generic implementation; this is by design. In fact, this can only be achieved in Java because of its type-erasure-based implementation.

You can only use a base non-generic interface (think IEnumerable<T> and IEnumerable).