C
C#4mo ago
stigzler

Inheritance best practice: test Type of subclass or use enum?

See attched UML. Just wondering what the best practice is? Is it better to test the Type of BaseEffect in code:
if (effect is BlurEffect)
if (effect is BlurEffect)
or to use an enum as per the UML:
if (effect.EffectType == EffectType.BlurEffect)
if (effect.EffectType == EffectType.BlurEffect)
No description
2 Replies
Angius
Angius3mo ago
I'd use pattern matching. You would have to cast the base class to the child class anyway, and pattern matching does it for you.
stigzler
stigzler3mo ago
Just read the microsoft pattern matching article and I'm sold! All makes the code super readable and nicely compact (aside from the arbitrary "_" as a discard token) Thanks