<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[Blogtronica]]></title><description><![CDATA[Blogtronica]]></description><link>https://blog.netronica.io/</link><image><url>https://blog.netronica.io/favicon.png</url><title>Blogtronica</title><link>https://blog.netronica.io/</link></image><generator>Ghost 5.81</generator><lastBuildDate>Sun, 29 Mar 2026 05:08:24 GMT</lastBuildDate><atom:link href="https://blog.netronica.io/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA["required" finally makes Nullable types good]]></title><description><![CDATA[<p>A bit of background. .NET has value types and reference types. Value types such as INT can not ever be null. If you type <code>int myInt = null</code> then you will get a compile time error .NET then introduced nullable value types allowing you to create something like <code>int? myInt</code> , and</p>]]></description><link>https://blog.netronica.io/nullable-reference-good-now/</link><guid isPermaLink="false">662bc2237fa679005d3114cb</guid><dc:creator><![CDATA[ivan]]></dc:creator><pubDate>Fri, 26 Apr 2024 15:49:33 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1647563099304-301a49c0e09d?crop=entropy&amp;cs=tinysrgb&amp;fit=max&amp;fm=jpg&amp;ixid=M3wxMTc3M3wwfDF8c2VhcmNofDQxfHxicm9rZW4lMjBjb21wdXRlcnxlbnwwfHx8fDE3MTQxNDcxNTZ8MA&amp;ixlib=rb-4.0.3&amp;q=80&amp;w=2000" medium="image"/><content:encoded><![CDATA[<img src="https://images.unsplash.com/photo-1647563099304-301a49c0e09d?crop=entropy&amp;cs=tinysrgb&amp;fit=max&amp;fm=jpg&amp;ixid=M3wxMTc3M3wwfDF8c2VhcmNofDQxfHxicm9rZW4lMjBjb21wdXRlcnxlbnwwfHx8fDE3MTQxNDcxNTZ8MA&amp;ixlib=rb-4.0.3&amp;q=80&amp;w=2000" alt="&quot;required&quot; finally makes Nullable types good"><p>A bit of background. .NET has value types and reference types. Value types such as INT can not ever be null. If you type <code>int myInt = null</code> then you will get a compile time error .NET then introduced nullable value types allowing you to create something like <code>int? myInt</code> , and  the compiler will force you to check if it is null before you can use it. Since any reference type could always be null there was no way to ever force them not to be null, and any usage could result in the dreaded <strong>&quot;Object reference not set to an instance of an object&quot; </strong>exception.</p><p>.NET core version 3 added support for C#8&apos;s Nullable reference types. To enable these project wide you would add the following to your csproj file</p><pre><code class="language-csharp">  &lt;PropertyGroup&gt;    
    &lt;Nullable&gt;enable&lt;/Nullable&gt;    
    &lt;LangVersion&gt;8&lt;/LangVersion&gt;
  &lt;/PropertyGroup&gt;
</code></pre><p>This would force the compiler to also consider all reference types as non nullable unless specified with a &quot;<strong>?</strong>&quot; but this often meant either some dirty workarounds (<code>public string Name { get; set; } = null!;</code>) or lots of boilerplate. e.g. to make the following POCO (plain old class object) null safe</p><pre><code class="language-csharp">class MyPoco
{
    public string Name { get; set; } 
    public Address Address { get; set; }
}</code></pre><p>We would need add more boilerplate code and always force it to be populated from a constructor</p><pre><code class="language-csharp">  class MyPoco
  {
      public MyPoco(string name, Address address)
      {
          Name = name;
          Address = address;
      }

      public string Name { get; set; } 
      public Address Address { get; set; }
  }</code></pre><p>Finally .NET 7 with C# Language version 11 introduced the required attribute!</p><p>We can now simply write:</p><pre><code class="language-csharp">  class MyPoco
  {
      public required string Name { get; set; } 
      public required Address Address { get; set; }
  }</code></pre><p>And the compiler will force us to populate all the required fields</p><figure class="kg-card kg-image-card"><img src="https://blog.netronica.io/content/images/2024/04/devenv_pw8Csm1KlH.png" class="kg-image" alt="&quot;required&quot; finally makes Nullable types good" loading="lazy" width="797" height="190" srcset="https://blog.netronica.io/content/images/size/w600/2024/04/devenv_pw8Csm1KlH.png 600w, https://blog.netronica.io/content/images/2024/04/devenv_pw8Csm1KlH.png 797w" sizes="(min-width: 720px) 720px"></figure>]]></content:encoded></item><item><title><![CDATA[Time based unit testing]]></title><description><![CDATA[<p>How do you test something with time, and you don&apos;t want to inject an ITime, resulting in dependencies heavy code?</p><pre><code class="language-csharp">public string GetGreeting()  
    {
        var response = &quot;Good &quot; + (DateTime.Now.Hour &lt; 12 ? &quot;morning!&quot; : &quot;afternoon!&quot;);
        return response;
    }
</code></pre><p>Simple, use a functional delagate where you</p>]]></description><link>https://blog.netronica.io/test-post/</link><guid isPermaLink="false">6606df7ca08f0b005ead852e</guid><dc:creator><![CDATA[ivan]]></dc:creator><pubDate>Sun, 27 Aug 2017 15:34:00 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1617299263454-3f1b584c89f3?crop=entropy&amp;cs=tinysrgb&amp;fit=max&amp;fm=jpg&amp;ixid=M3wxMTc3M3wwfDF8c2VhcmNofDQ5fHx0aW1lfGVufDB8fHx8MTcxMTczNzEzNXww&amp;ixlib=rb-4.0.3&amp;q=80&amp;w=2000" medium="image"/><content:encoded><![CDATA[<img src="https://images.unsplash.com/photo-1617299263454-3f1b584c89f3?crop=entropy&amp;cs=tinysrgb&amp;fit=max&amp;fm=jpg&amp;ixid=M3wxMTc3M3wwfDF8c2VhcmNofDQ5fHx0aW1lfGVufDB8fHx8MTcxMTczNzEzNXww&amp;ixlib=rb-4.0.3&amp;q=80&amp;w=2000" alt="Time based unit testing"><p>How do you test something with time, and you don&apos;t want to inject an ITime, resulting in dependencies heavy code?</p><pre><code class="language-csharp">public string GetGreeting()  
    {
        var response = &quot;Good &quot; + (DateTime.Now.Hour &lt; 12 ? &quot;morning!&quot; : &quot;afternoon!&quot;);
        return response;
    }
</code></pre><p>Simple, use a functional delagate where you can replace the implementation.</p><pre><code class="language-csharp">public static class DateTimeProvider  
{
    public static Func&lt;DateTime&gt; Now = () =&gt; DateTime.Now;
}

public class Worker  
{
    public string GetGreeting()
    {
        var response = &quot;Good &quot; + (DateTimeProvider.Now().Hour &lt; 12 ? &quot;morning!&quot; : &quot;afternoon!&quot;);
        return response;
    }
}
</code></pre><p>And now it is testable!</p><pre><code class="language-csharp">[Theory]
[InlineData(&quot;2060-10-1 00:00:00&quot;, &quot;morning&quot;)]
[InlineData(&quot;2060-10-1 11:59:59&quot;, &quot;morning&quot;)]
[InlineData(&quot;2060-10-1 12:00:00&quot;, &quot;afternoon&quot;)]
public void TestGreeting(string forDate, string expect)  
{
    DateTimeProvider.Now = () =&gt; DateTime.Parse(forDate);
    var sut = new Worker();
    var greeting = sut.GetGreeting();
    Assert.Contains(expect, greeting);
}
</code></pre>]]></content:encoded></item></channel></rss>