When to use a static helper class vs extension methods
When adding a static class, what criteria do you use to determine if it should be a helper class or an extension class?
For example, I have a recursive hierarchical data structure (has Children list of more of itself) and I want to write methods to help searching and iterating through it.
I could have a helper class like:
or a extension class like:
7 Replies
Or neither: have the method on the
Root
type directly.Root is from an external library
Performance wise, I don't know if there's much of a difference. Maybe you can create a small side project to determine operating speeds.
For me personally, I would go for extension methods. I like how intellesense detects them. I've always found Helper classes/methods like 'bandaids' to poorly planned code structure.
utility classes like that aren't uncommon. think
Console
, File
, Path
, etc. or even better examples; MemoryMarshal
, CollectionsMarshal
, BinaryPrimitives
it depends on the usage and the goal of your library. is this class really meant to be public?
if it's only for your own utility, i would make the class internal
and an extension class.
if you're providing this utility to your library's users, then i would probably consider making it a utility class.
just my opinionUnknown User•5w ago
Message Not Public
Sign In & Join Server To View
Not just near
Awesome, thanks everyone!