Get inherited type from list of "parent" type

So, essentially, I have a list of the class “effect”, and want to get all of those that is of the inheriting type “SkillBonusEffect”. So I did this:

public List<EffectSkillBonus> getSkillEffects() {
        List<EffectSkillBonus> output = new List<EffectSkillBonus>();
        foreach(var effect in this.Effects) {
            if (effect.GetType() == typeof(EffectSkillBonus)) {
                output.Add(effect);
            }
        }
       
        return output;
    }

But, it understandably, gives me the error:
Argument 1: cannot convert from 'Effect' to 'EffectSkillBonus'

What is the best way to do this?
It is important that it returns a list of EffectSkillBonus.

the syntax ‘x is T y’ returns true if x is the type T, and also casts x to T and assigns that to the variable y:

if (effect is EffectSkillBonus skillBonus)
    output.Add(skillBonus);
4 Likes

Fantastic, thanks for the help! Solved the issue, plus taught me a great way to type check.

yep good answer
it’s called pattern matching, for the reference