<?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: Yuri Eastwood</title>
    <description>The latest articles on DEV Community by Yuri Eastwood (@yurieastwood).</description>
    <link>https://dev.to/yurieastwood</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%2F463716%2F34fb4a9c-f398-49d5-94af-61bd6eefb15b.jpeg</url>
      <title>DEV Community: Yuri Eastwood</title>
      <link>https://dev.to/yurieastwood</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yurieastwood"/>
    <language>en</language>
    <item>
      <title>C# 9.0 Perks</title>
      <dc:creator>Yuri Eastwood</dc:creator>
      <pubDate>Sat, 14 Nov 2020 05:59:48 +0000</pubDate>
      <link>https://dev.to/yurieastwood/c-9-0-perks-19me</link>
      <guid>https://dev.to/yurieastwood/c-9-0-perks-19me</guid>
      <description>&lt;p&gt;Hello everyone!&lt;/p&gt;

&lt;p&gt;C# 9.0 is the new kid on the block for language enthusiasts.&lt;/p&gt;

&lt;p&gt;There are quite a few new "goodies" in it, here I'll talk a little bit about some that caught my attention, be it because it's something that will make my everyday easier or something that community has talked a lot about it and it's finally here!&lt;/p&gt;

&lt;p&gt;I'll cover &lt;strong&gt;Init-only properties and accessors&lt;/strong&gt;, &lt;strong&gt;target-typed new expressions&lt;/strong&gt; and &lt;strong&gt;records &amp;amp; with-expressions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Got all of those objects that should be set only in initialization? Problem solved!&lt;br&gt;
The &lt;strong&gt;init accessor&lt;/strong&gt; was just introduced to make your day easier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Public class Product
{
    public string? Name { get; init; }
    public string? Brand { get; init; }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the aforementioned declaration any subsequent assignment to those properties will throw a compile time error!&lt;br&gt;
This means that initialization onwards the state of the object is totally protected from mutation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var product = new Product { Name = "Tomato", Brand = "XYZ" }; // YAY!
product.Brand = "GEEZ"; // NOPZ!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And with &lt;strong&gt;target-type new expressions&lt;/strong&gt; we can kiss goodbye to verbose declarations!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product product = new { Name = "Tomato", Brand = "XYZ" };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which would be especially nicer if we were dealing with arrays or lists!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product[] products = { 
    new { Name = "Tomato", Brand = "XYZ" }, 
    new { Name = "Potato", Brand = "XYZ" }, 
    new { Name = "Tomato", Brand = "GEEZ" }, 
    new { Name = "Potato", Brand = "GEEZ" }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We do have a trade-off between var and target-type new expressions, but the freedom to decide what's better in each situation is what thrills me. &lt;/p&gt;

&lt;p&gt;Continuing on our previous subject: want the whole object to be immutable, just like a value type behavior? Combine it with &lt;strong&gt;Record class&lt;/strong&gt;!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Public record Product
{
    public string? Name { get; init; }
    public string? Brand { get; init; }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In these cases we normally represent new states by creating a whole new object based on the values of existing ones. And this is where &lt;strong&gt;With-expressions&lt;/strong&gt; come into place!&lt;/p&gt;

&lt;p&gt;We can create the new object by only stating what's different:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product product = new { Name = "Tomato", Brand = "XYZ" };
var otherProduct = product with { Brand = "GEEZ" };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are many other features that might help you out such as static anonymous functions, covariant returns and improved pattern matching. To learn more take a look in the &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9" rel="noopener noreferrer"&gt;"What's new in C# 9.0" documentation&lt;/a&gt;.&lt;/p&gt;

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