C
C#8mo 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
Angius8mo 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
stigzlerOP8mo 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
Want results from more Discord servers?
Add your server