TIPS FOR DEVELOPERS
The Most Overlooked Collection Feature in C#
Making your code safer, maintainable, and enjoyable using custom classes with collection initialization
I’m sure even non-.NET developers will find the ideas useful.
For the uninitiated or anyone starting to pick up C#, collection initialization is one of those need-to-know language features C# has to offer.
The greatest thing about C# is how flexible the language is — making it likely the most enjoyable language to work with. Microsoft keeps adding new, awesome features to the language and .NET ecosystem. Even so many features that we often miss a few of the really handy, convenient ones.
Writing a custom class leveraging the collection initializer is one of the most overlooked features. This is even despite the collection initializer’s heavy use when initializing e.g. the
In doubt of what I’m talking about? Here’s an example of the collection initializer syntax.
Just so everyone is on the same page, both the examples initialize a list and add three elements to it. We can build classes that allow for the shorthand syntax ourselves.
So, you might ask why we’d even want to make our own collection classes
While it’s completely fine to use regular lists to hold values, you’d often want more control and to provide validation before adding an item to the list.
You see, the
List<T> interface has an
Add() method. This method is completely indifferent as to what you pass it, as long as the value is of a certain type.
This means something insane as demonstrated in the snippet below is allowed. The code compiles but will explode on you.
If you’re stuck in 2018, this might acceptable to you. But please, move on and refine your skills.
It’s so easy to avoid mistakes like this. By spending a few minutes, you can easily make your program safer, developers happier, and improve the overall code quality.
Time for the fun part
So, we’re going to build a custom class used to hold comment objects. For simplicity — and because I lack imagination — we call this class
We’re creating this class to avoid the unfortunate event of a junior developer adding null to a collection.
We have implemented the comment collection below. We’ll go thru the code in a second. First, try to understand what’s going on.
You see, we still expose an
Add() method. However, this time, we’re actually checking the value provided to it. We’re no longer completely oblivious as to what’s going on.
We’re still, under the hood, working with a regular list object. But it’s private. Inaccessible to the chaos surrounding it, known as web development.
This approach also allows us to add meaningful methods to our comments collection. Instead of the client having to define and perform queries, we expose them directly from the collection. Immensely improving the quality of life for developers.
Here, we’ve added a method called
WithHighestClapCount to the class. Now, every user of the class no longer has to know how to get the comment with the highest clap count. It’s provided to them.
Another cool thing is, we can even iterate over the
CommentsCollection object itself. Just as if it was a regular list.
Resources for the curious
--------------------------Object and Collection Initializers in C# by Microsoft
Factory Pattern Without Switch, This Is How It Should Be Done
My approach to creating easily extensible factory classes without logical statements such as switch and if-else
Nicklas Millard is a software development engineer in one of the fastest-growing banks, building mission-critical financial services infrastructure.
Previously, he was a Big4 Senior Tech Consultant developing software for commercial clients and government institutions.
Connect on LinkedIn