C#C
C#3y ago
Clonkex

✅ Return List<Entity> from method with return type of List<T> where T : Entity

This is probably a dumb question, but why can't I do this?

public static readonly Dictionary<Type, List<Entity>> EntitiesByType = new();

public static List<T> GetEntitiesByType<T>() where T : Entity
{
    if(EntitiesByType.TryGetValue(typeof(T), out var entities))
    {
        return entities;
    }
    return new();
}


I can't get my head around why this isn't allowed.
Was this page helpful?