avatarNicklas Millard

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2626

Abstract

on></figure><p id="7c77">If you’re stuck in 2018, this might acceptable to you. But please, move on and refine your skills.</p><p id="3eba">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.</p><h2 id="fd3b">Time for the fun part</h2><p id="fdcc">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 <code>CommentCollection</code>.</p><p id="5997">We’re creating this class to avoid the unfortunate event of a junior developer adding null to a collection.</p><p id="48be">We have implemented the comment collection below. We’ll go thru the code in a second. First, try to understand what’s going on.</p><figure id="5c90"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*WjsmWIarm51xejnMbFrpnA.png"><figcaption>CommentCollection class</figcaption></figure><p id="55d4">You see, we still expose an <code>Add()</code> 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.</p><p id="11e2">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.</p><p id="3bdd">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.</p><figure id="4f6e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*3VBOvjfivrR0dtjnKtBMhA.png"><figcaption></figcaption></figure><p id="7a86">Here, we’ve added a method called <code>WithHighestClapCount</code> 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.</p><p id="b10d">Another cool thing is, we can even iterate over the <code>CommentsCollection</code> object itself. Just as if it was a regular list.</p><div id="b664"><pre><span class="hljs-section">Resources for the curious --------------------------</span></pre></div><div id="ebb5"><pre><span class="hljs-built_in">Object</span> <span class="hljs-keyword">and</span> Collection Initializers <span class="hljs-keyword">in</span> C<span class="hljs-comment"># by Microsoft</span></pre></div><figure id="5c4b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*hBvjghpeKQsRVKt_.png"><figcaption></figcaption></figure><div id="0faa" class="lin

Options

k-block"> <a href="https://readmedium.com/factory-pattern-without-switch-this-is-how-it-should-be-done-cd895e356f44"> <div> <div> <h2>Factory Pattern Without Switch, This Is How It Should Be Done</h2> <div><h3>My approach to creating easily extensible factory classes without logical statements such as switch and if-else</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*a1kITu1qyNi9sF19GFqCsA.png)"></div> </div> </div> </a> </div><div id="d239" class="link-block"> <a href="https://readmedium.com/5-ways-to-replace-if-else-statements-857c0ff19357"> <div> <div> <h2>Better Software Without If-Else</h2> <div><h3>5 Ways to Replace If-Else. Beginner to advanced examples</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*3Ju0VYbFi0w0rVYt9RT_vw.png)"></div> </div> </div> </a> </div><div id="0b46" class="link-block"> <a href="https://readmedium.com/stop-using-if-else-statements-f4d2323e6e4"> <div> <div> <h2>Stop Using If-Else Statements</h2> <div><h3>Write clean, maintainable code without if-else.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*xUWTgHxBR4P_lYWzqGgImQ.png)"></div> </div> </div> </a> </div><figure id="b109"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*LZ0-rAawOB4iv6uI.png"><figcaption></figcaption></figure><p id="cc49"><b>Nicklas Millard</b> is a software development engineer in one of the fastest-growing banks, building mission-critical financial services infrastructure.</p><p id="7bfa">Previously, he was a Big4 Senior Tech Consultant developing software for commercial clients and government institutions.</p><blockquote id="71d7"><p><a href="https://www.youtube.com/channel/UCaUy83EAkVdXsZjF3xGSvMw">New YouTube Channel (@Nicklas Millard)</a></p></blockquote><blockquote id="f1a4"><p><i>Connect on <a href="https://www.linkedin.com/in/nicklasmillard/">LinkedIn</a></i></p></blockquote></article></body>

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

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

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.

If you’re a visual learner, then you may want to see this short video of how I implemented a class that uses the collection initializer syntax.

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.

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

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

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

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.

New YouTube Channel (@Nicklas Millard)

Connect on LinkedIn

Technology
Software Development
Software Engineering
Web Development
Programming
Recommended from ReadMedium