<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tommy Williams</title>
    <description>The latest articles on DEV Community by Tommy Williams (@twwilliams).</description>
    <link>https://dev.to/twwilliams</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F47841%2Fd88cb401-d5fe-405e-973c-c3963572614a.jpg</url>
      <title>DEV Community: Tommy Williams</title>
      <link>https://dev.to/twwilliams</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/twwilliams"/>
    <language>en</language>
    <item>
      <title>Simplify foreach loops with the null-coalescing operator</title>
      <dc:creator>Tommy Williams</dc:creator>
      <pubDate>Sun, 31 Jan 2021 14:51:27 +0000</pubDate>
      <link>https://dev.to/twwilliams/simplify-foreach-loops-with-the-null-coalescing-operator-4bl</link>
      <guid>https://dev.to/twwilliams/simplify-foreach-loops-with-the-null-coalescing-operator-4bl</guid>
      <description>&lt;p&gt;Do you ever get tired of writing code like this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listOfStrings&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;listOfStrings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wouldn't it be nice if the &lt;code&gt;foreach&lt;/code&gt; loop automatically checked for null and skipped the loop the same way it does when there are no items?&lt;/p&gt;

&lt;p&gt;Here's where the null-coalescing operator can help:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;listOfStrings&lt;/span&gt; &lt;span class="p"&gt;??&lt;/span&gt; &lt;span class="n"&gt;Enumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;())&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lots more in the &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator"&gt;official docs from Microsoft&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
    </item>
    <item>
      <title>How I misrepresented a C# 9 code feature</title>
      <dc:creator>Tommy Williams</dc:creator>
      <pubDate>Fri, 13 Nov 2020 14:36:04 +0000</pubDate>
      <link>https://dev.to/twwilliams/how-i-misrepresented-a-c-9-code-feature-1a3g</link>
      <guid>https://dev.to/twwilliams/how-i-misrepresented-a-c-9-code-feature-1a3g</guid>
      <description>&lt;p&gt;Yesterday, I complained about some of the new syntax in C# 9 and, in so doing, made the opposite point I intended. Let me explain.&lt;/p&gt;

&lt;p&gt;First the tweet:&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--qOvPJXnc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/1078718042157142016/cG_g1ZxO_normal.jpg" alt="Tommy Williams profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Tommy Williams
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        &lt;a class="comment-mentioned-user" href="https://dev.to/twwilliams"&gt;@twwilliams&lt;/a&gt;

      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P4t6ys1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      OK C# 9 fans.&lt;br&gt;&lt;br&gt;How is this:&lt;br&gt;&lt;br&gt;if (listOfThings is null or { Count: 0 }) { ... }&lt;br&gt;&lt;br&gt;better than what we used to do?&lt;br&gt;&lt;br&gt;if (listOfThings?.Count == 0) { ... }&lt;br&gt;&lt;br&gt;I'm seeing that first pattern all over the place from the &lt;a href="https://twitter.com/hashtag/dotnet"&gt;#dotnet&lt;/a&gt; team and don't see the value except to show off new features.
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      19:07 PM - 12 Nov 2020
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1326964779357884416" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-reply-action.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1326964779357884416" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-retweet-action.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      1
      &lt;a href="https://twitter.com/intent/like?tweet_id=1326964779357884416" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-like-action.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
      7
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;The big problem here is that my pre-C# 9 example doesn't do the same thing as the C# 9 code.&lt;/p&gt;

&lt;p&gt;To get the same behavior, I needed to write this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listOfThings&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;listOfThings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see a running example with various types of checks at &lt;a href="https://dotnetfiddle.net/VNO2Sv"&gt;https://dotnetfiddle.net/VNO2Sv&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What I got wrong
&lt;/h2&gt;

&lt;p&gt;I was so focused on the Count part that I forgot about the null check part. The C# 9 example makes that very clear.&lt;/p&gt;

&lt;p&gt;And in my .NET Fiddle example, you can see that the syntax I was using doesn't work at all if you are instead checking for an enumerable with items in it when the List is null:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;listOfThings&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listOfThings&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That will give you a false positive (indeed, &lt;code&gt;.Count&lt;/code&gt; on a null object is not equal to 0) but then you'll have a NullReferenceException if you try to operate on &lt;code&gt;listOfThings&lt;/code&gt; in the if block.&lt;/p&gt;

&lt;p&gt;Just a little reminder that the language designers aren't just making changes for the heck of it.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
    </item>
  </channel>
</rss>
