简体   繁体   中英

Does a C# Generics class casting similar to Java Generics class casting exist?

The code below is what I use for a java program to cast a generic to a specific. I understand there is no wild cards in C# but was hoping somebody can point me in the right direction as how I can modify the code to be similar to java:

public <T extends GameComponent<?>>T getComponent(int id, Class<T> type){
    return type.cast(components.get(id));
}

Looks as something like:

public T GetComponent<T>(int id) where T : GameComponent
{
    return components[id] as T;
}

Thanks for everyone in the comments, helping me fix my solution (as I was way off). I couldn't have been here without you guys. And I'll miss you.

<3

But really, thanks :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM