Image for post
Image for post

TIPS FOR DEVELOPERS

The Most Overlooked Collection Feature in C#

Making your code safer, maintainable, and enjoyable using custom classes with collection initialization

Nicklas Millard
Jul 26, 2020 · 4 min read

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 class.

In doubt of what I’m talking about? Here’s an example of the collection initializer syntax.

Collection initialization vs regular method calling
Collection initialization vs regular method calling
Collection initialization vs regular method calling

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 interface has an 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.

Accessing null object from list
Accessing null object from list
How to ruin an application

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.

CommentCollection class
CommentCollection class
CommentCollection class

You see, we still expose an 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.

Image for post
Image for post

Here, we’ve added a method called 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 object itself. Just as if it was a regular list.

Resources for the curious
--------------------------
Object and Collection Initializers in C# by Microsoft
Image for post
Image for post
Image for post
Image for post

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

The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Nicklas Millard

Written by

Tech writer with 701K+ views. Sharing my opinion and what I learn. Danish C# backend engineer in FinTech. Ex Big4 senior tech consultant.

The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Nicklas Millard

Written by

Tech writer with 701K+ views. Sharing my opinion and what I learn. Danish C# backend engineer in FinTech. Ex Big4 senior tech consultant.

The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store