© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
54 replies
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();
}
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Cannot return null from an action method with a return type of 'Microsoft.AspNetCore.Mvc.JsonResult'
C#CC# / help
11mo ago
❔ Entity Framework 7 - Where to call EnsureCreared method
C#CC# / help
3y ago
❔ Doing post method with entity framework
C#CC# / help
4y ago
❔ Return two values from a method
C#CC# / help
3y ago
Next page