<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C#8.0 Archives - CodeJourney.net</title>
	<atom:link href="https://www.codejourney.net/tag/c8-0/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codejourney.net/tag/c8-0/</link>
	<description>Pragmatic full stack software development</description>
	<lastBuildDate>Tue, 19 Feb 2019 14:40:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/10/cropped-512px-na-512px-JPEG-BEZ-NAPISU-1.jpg?fit=32%2C32&#038;ssl=1</url>
	<title>C#8.0 Archives - CodeJourney.net</title>
	<link>https://www.codejourney.net/tag/c8-0/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">123174533</site>	<item>
		<title>C# 8: Default Interface Methods Implementation</title>
		<link>https://www.codejourney.net/csharp-8-default-interface-methods/</link>
					<comments>https://www.codejourney.net/csharp-8-default-interface-methods/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Wed, 20 Feb 2019 14:00:25 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C#8.0]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=3495</guid>

					<description><![CDATA[<p>Continuing to examine new C# 8 features, today we are taking a look at a bit controversial one. It&#8217;s a possibility to provide default interface methods implementation. How will this possibility change the way we write C#? Why is it being introduced? I&#8217;ll try to address these questions today 🙂 Interfaces today As we all&#8230;</p>
<p>The post <a href="https://www.codejourney.net/csharp-8-default-interface-methods/">C# 8: Default Interface Methods Implementation</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Continuing to examine <a rel="noreferrer noopener" aria-label="new C# 8 features (opens in a new tab)" href="https://www.codejourney.net/tag/c8-0/" target="_blank">new C# 8 features</a>, today we are taking a look at a bit controversial one. It&#8217;s a possibility to provide <strong>default interface methods</strong> <strong>implementation</strong>. </p>



<p>How will this possibility change the way we write C#? Why is it being introduced? I&#8217;ll try to address these questions today <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-3495"></span>



<h2 class="wp-block-heading">Interfaces today</h2>



<p>As we all know from the current version of C#, interfaces allow to define a kind of contract, which must be respected by a class implementing it. Today interfaces can only contain definitions of methods (with no body). These methods are then actually implemented by concrete classes. If a class doesn&#8217;t implement all of its interface&#8217;s methods, compiler produces an error.</p>



<p>What&#8217;s problematic in this approach? Assuming that you have the following interface and class(es) implementing it:</p>



<figure class="wp-block-embed aligncenter"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist94701778" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-interfaceandclass-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="InterfaceAndClass.cs content, created by dsibinski on 05:42PM on February 17, 2019."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="InterfaceAndClass.cs">
        <tr>
          <td id="file-interfaceandclass-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-interfaceandclass-cs-LC1" class="blob-code blob-code-inner js-file-line">  interface IDeveloper</td>
        </tr>
        <tr>
          <td id="file-interfaceandclass-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-interfaceandclass-cs-LC2" class="blob-code blob-code-inner js-file-line">  {</td>
        </tr>
        <tr>
          <td id="file-interfaceandclass-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-interfaceandclass-cs-LC3" class="blob-code blob-code-inner js-file-line">      void LearnNewLanguage(string language, DateTime dueDate);</td>
        </tr>
        <tr>
          <td id="file-interfaceandclass-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-interfaceandclass-cs-LC4" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
        <tr>
          <td id="file-interfaceandclass-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-interfaceandclass-cs-LC5" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-interfaceandclass-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-interfaceandclass-cs-LC6" class="blob-code blob-code-inner js-file-line">  class BackendDev : IDeveloper</td>
        </tr>
        <tr>
          <td id="file-interfaceandclass-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-interfaceandclass-cs-LC7" class="blob-code blob-code-inner js-file-line">  {</td>
        </tr>
        <tr>
          <td id="file-interfaceandclass-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-interfaceandclass-cs-LC8" class="blob-code blob-code-inner js-file-line">      public void LearnNewLanguage(string language, DateTime dueDate)</td>
        </tr>
        <tr>
          <td id="file-interfaceandclass-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-interfaceandclass-cs-LC9" class="blob-code blob-code-inner js-file-line">      {</td>
        </tr>
        <tr>
          <td id="file-interfaceandclass-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-interfaceandclass-cs-LC10" class="blob-code blob-code-inner js-file-line">          // Learning new language&#8230;</td>
        </tr>
        <tr>
          <td id="file-interfaceandclass-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-interfaceandclass-cs-LC11" class="blob-code blob-code-inner js-file-line">      }</td>
        </tr>
        <tr>
          <td id="file-interfaceandclass-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-interfaceandclass-cs-LC12" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/7b5c3454e4976c1b96ac97500be949fc/raw/cb6412facda3f0b612bc289084dcb211536a6e2c/InterfaceAndClass.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/7b5c3454e4976c1b96ac97500be949fc#file-interfaceandclass-cs" class="Link--inTextBlock">
          InterfaceAndClass.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>


<p>With time, you may want to extend <span style="color: #ff6600;">IDeveloper</span> interface by adding a new method, for instance:</p>


<figure class="wp-block-embed aligncenter"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist94701794" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-interfacemodified-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="InterfaceModified.cs content, created by dsibinski on 05:44PM on February 17, 2019."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="InterfaceModified.cs">
        <tr>
          <td id="file-interfacemodified-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-interfacemodified-cs-LC1" class="blob-code blob-code-inner js-file-line">  interface IDeveloper</td>
        </tr>
        <tr>
          <td id="file-interfacemodified-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-interfacemodified-cs-LC2" class="blob-code blob-code-inner js-file-line">  {</td>
        </tr>
        <tr>
          <td id="file-interfacemodified-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-interfacemodified-cs-LC3" class="blob-code blob-code-inner js-file-line">      void LearnNewLanguage(string language, DateTime dueDate);  </td>
        </tr>
        <tr>
          <td id="file-interfacemodified-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-interfacemodified-cs-LC4" class="blob-code blob-code-inner js-file-line">      void LearnNewLanguage(string language); // new method added</td>
        </tr>
        <tr>
          <td id="file-interfacemodified-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-interfacemodified-cs-LC5" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/509cee33619a492fe29555d0039230f9/raw/557c447d23512860c9af5b2563b3d17d7256d598/InterfaceModified.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/509cee33619a492fe29555d0039230f9#file-interfacemodified-cs" class="Link--inTextBlock">
          InterfaceModified.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>Now you have a problem, because the <strong>new method must be implemented in all places where the interface was used</strong>. Compiler gives you an error as long as it&#8217;s not done: </p>



<figure class="wp-block-image"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="900" height="400" data-attachment-id="3508" data-permalink="https://www.codejourney.net/csharp-8-default-interface-methods/interfacemembernotimplemented-2/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/InterfaceMemberNotImplemented-1.png?fit=900%2C400&amp;ssl=1" data-orig-size="900,400" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="InterfaceMemberNotImplemented" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/InterfaceMemberNotImplemented-1.png?fit=900%2C400&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/InterfaceMemberNotImplemented-1.png?resize=900%2C400&#038;ssl=1" alt="C# 8: Default Interface Methods Implementation: C# compilation error - interface member not implemented" class="wp-image-3508" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/InterfaceMemberNotImplemented-1.png?w=900&amp;ssl=1 900w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/InterfaceMemberNotImplemented-1.png?resize=300%2C133&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/InterfaceMemberNotImplemented-1.png?resize=768%2C341&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/InterfaceMemberNotImplemented-1.png?resize=676%2C300&amp;ssl=1 676w" sizes="(max-width: 900px) 100vw, 900px" /><figcaption>C# compilation error &#8211; interface member not implemented</figcaption></figure>


<p>If your interface is used in many places, also by another teams or developers (which is often the case in reality), <strong>this is a breaking change <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f641.png" alt="🙁" class="wp-smiley" style="height: 1em; max-height: 1em;" /></strong></p>
<p>It may be really painful, depending on how extensively your interface is used. In any case, all classes based on the <span style="color: #ff6600;">IDeveloper</span> interface must implement the new <span style="color: #ff6600;">LearnNewLanguage(string language)</span> method. Only then the code compiles.</p>
<p>One of the reasons for&nbsp;<a href="https://github.com/dotnet/csharplang/issues/52" target="_blank" rel="noopener">introducing into C# default interface methods</a> is to avoid such breaking changes scenarios <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>


<h2 class="wp-block-heading">Default Interface Methods</h2>



<p>As we can read in the <a rel="noreferrer noopener" aria-label="feature's design notes (opens in a new tab)" href="https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md" target="_blank">feature&#8217;s design notes</a>, an inspiration for it is a similar concept already alive in Java &#8211; <em><a rel="noreferrer noopener" aria-label="Default Methods (opens in a new tab)" href="https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html" target="_blank">Default Methods</a></em>.</p>



<p>The main idea of this new C# feature is a possibility to <strong>provide a default implementation of an interface&#8217;s method</strong>.</p>



<h3 class="wp-block-heading">Realization in C# 8.0</h3>



<p>In our example using C# 8.0 we&#8217;ll be able to implement it as follows:</p>



<figure class="wp-block-embed aligncenter"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist94702032" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-defaultinterfacemethod-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="DefaultInterfaceMethod.cs content, created by dsibinski on 06:08PM on February 17, 2019."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="DefaultInterfaceMethod.cs">
        <tr>
          <td id="file-defaultinterfacemethod-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-defaultinterfacemethod-cs-LC1" class="blob-code blob-code-inner js-file-line">  interface IDeveloper</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-defaultinterfacemethod-cs-LC2" class="blob-code blob-code-inner js-file-line">  {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-defaultinterfacemethod-cs-LC3" class="blob-code blob-code-inner js-file-line">      void LearnNewLanguage(string language, DateTime dueDate);</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-defaultinterfacemethod-cs-LC4" class="blob-code blob-code-inner js-file-line">      void LearnNewLanguage(string language)</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-defaultinterfacemethod-cs-LC5" class="blob-code blob-code-inner js-file-line">      {   </td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-defaultinterfacemethod-cs-LC6" class="blob-code blob-code-inner js-file-line">          // default implementation</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-defaultinterfacemethod-cs-LC7" class="blob-code blob-code-inner js-file-line">          LearnNewLanguage(language, DateTime.Now.AddMonths(6));</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-defaultinterfacemethod-cs-LC8" class="blob-code blob-code-inner js-file-line">      }</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-defaultinterfacemethod-cs-LC9" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/1c6e54a566eb9854cd18f7a535fb116b/raw/8285fe2333fd7ba4a713d9953dc19e9c50fc1c71/DefaultInterfaceMethod.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/1c6e54a566eb9854cd18f7a535fb116b#file-defaultinterfacemethod-cs" class="Link--inTextBlock">
          DefaultInterfaceMethod.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>


<p>In that case, our class implementing the <span style="color: #ff6600;">IDeveloper</span> interface can legally not implement this new method and there will be no compilation error:</p>


<figure class="wp-block-embed aligncenter"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist94702168" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-defaultinterfacemethod_usage-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="DefaultInterfaceMethod_usage.cs content, created by dsibinski on 06:22PM on February 17, 2019."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="DefaultInterfaceMethod_usage.cs">
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC1" class="blob-code blob-code-inner js-file-line">  interface IDeveloper</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC2" class="blob-code blob-code-inner js-file-line">  {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC3" class="blob-code blob-code-inner js-file-line">      void LearnNewLanguage(string language, DateTime dueDate);</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC4" class="blob-code blob-code-inner js-file-line">      void LearnNewLanguage(string language)</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC5" class="blob-code blob-code-inner js-file-line">      {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC6" class="blob-code blob-code-inner js-file-line">          // default implementation</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC7" class="blob-code blob-code-inner js-file-line">          LearnNewLanguage(language, DateTime.Now.AddMonths(6));</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC8" class="blob-code blob-code-inner js-file-line">      }</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC9" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC10" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC11" class="blob-code blob-code-inner js-file-line">  class BackendDev : IDeveloper // compiles OK</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC12" class="blob-code blob-code-inner js-file-line">  {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC13" class="blob-code blob-code-inner js-file-line">      public void LearnNewLanguage(string language, DateTime dueDate)</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC14" class="blob-code blob-code-inner js-file-line">      {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC15" class="blob-code blob-code-inner js-file-line">          // Learning new language&#8230;</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC16" class="blob-code blob-code-inner js-file-line">      }</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC17" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC18" class="blob-code blob-code-inner js-file-line">  </td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC19" class="blob-code blob-code-inner js-file-line">  class Program</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC20" class="blob-code blob-code-inner js-file-line">  {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC21" class="blob-code blob-code-inner js-file-line">      static void Main(string[] args)</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L22" class="blob-num js-line-number js-blob-rnum" data-line-number="22"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC22" class="blob-code blob-code-inner js-file-line">      {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L23" class="blob-num js-line-number js-blob-rnum" data-line-number="23"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC23" class="blob-code blob-code-inner js-file-line">          IDeveloper dev = new BackendDev();</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L24" class="blob-num js-line-number js-blob-rnum" data-line-number="24"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC24" class="blob-code blob-code-inner js-file-line">          dev.LearnNewLanguage(&quot;Rust&quot;); // OK &#8211; default implementation of</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L25" class="blob-num js-line-number js-blob-rnum" data-line-number="25"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC25" class="blob-code blob-code-inner js-file-line">                                        // IDeveloper.LearnNewLanguage(string language) is called</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L26" class="blob-num js-line-number js-blob-rnum" data-line-number="26"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC26" class="blob-code blob-code-inner js-file-line">      }</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage-cs-L27" class="blob-num js-line-number js-blob-rnum" data-line-number="27"></td>
          <td id="file-defaultinterfacemethod_usage-cs-LC27" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/1a1af2c889fe5f101c59e65cf1107b3b/raw/aa6a0e5f0be3e2696d0b978410bf44d1fece564d/DefaultInterfaceMethod_usage.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/1a1af2c889fe5f101c59e65cf1107b3b#file-defaultinterfacemethod_usage-cs" class="Link--inTextBlock">
          DefaultInterfaceMethod_usage.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>


<p>Notice that it only works when&nbsp;<span style="color: #ff6600;">BackendDev</span> is contextually treated as <span style="color: #ff6600;">IDeveloper</span>. It means that a class doesn&#8217;t inherit members from the interfaces it implements:</p>


<figure class="wp-block-embed aligncenter"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist94704015" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-defaultinterfacemethod_usage_nok-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="DefaultInterfaceMethod_usage_NOK.cs content, created by dsibinski on 09:19PM on February 17, 2019."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="DefaultInterfaceMethod_usage_NOK.cs">
        <tr>
          <td id="file-defaultinterfacemethod_usage_nok-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-defaultinterfacemethod_usage_nok-cs-LC1" class="blob-code blob-code-inner js-file-line">  BackendDev dev = new BackendDev();	          </td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage_nok-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-defaultinterfacemethod_usage_nok-cs-LC2" class="blob-code blob-code-inner js-file-line">  dev.LearnNewLanguage(&quot;Rust&quot;); // compilation error: class &#39;BackendDev&#39; </td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_usage_nok-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-defaultinterfacemethod_usage_nok-cs-LC3" class="blob-code blob-code-inner js-file-line">                                // does not contain a member &#39;LearnNewLanguage&#39;</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/4a9a2ccf9163390d9c52b86e7491a055/raw/0572886c2ec17de6a38719a3e96c1d2a373964b4/DefaultInterfaceMethod_usage_NOK.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/4a9a2ccf9163390d9c52b86e7491a055#file-defaultinterfacemethod_usage_nok-cs" class="Link--inTextBlock">
          DefaultInterfaceMethod_usage_NOK.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<h3 class="wp-block-heading">What about multiple inheritance?</h3>



<p>As some of you probably realized, this new feature exposes C# to a <a rel="noreferrer noopener" aria-label="multiple inheritance problem (opens in a new tab)" href="https://en.wikipedia.org/wiki/Multiple_inheritance" target="_blank">multiple inheritance problem</a> (also known as <em>Diamond of Death</em>). So far C# didn&#8217;t have this issue, because we can only inherit from a single class. We can implement multiple interfaces, but so far the interfaces couldn&#8217;t provide methods&#8217; implementation. Because of that, there was no <em>Diamond of Death</em> possible. Soon it&#8217;s gonna change <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h4 class="wp-block-heading"><strong>Diamond of Death in C# 8</strong></h4>



<p>In case of C# default interface methods we may illustrate this issue with the following code:</p>



<figure class="wp-block-embed aligncenter"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist94702504" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-defaultinterfacemethod_diamondofdeath-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="DefaultInterfaceMethod_DiamondOfDeath.cs content, created by dsibinski on 06:53PM on February 17, 2019."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="DefaultInterfaceMethod_DiamondOfDeath.cs">
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC1" class="blob-code blob-code-inner js-file-line">  interface IDeveloper</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC2" class="blob-code blob-code-inner js-file-line">  {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC3" class="blob-code blob-code-inner js-file-line">      void LearnNewLanguage(string language)</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC4" class="blob-code blob-code-inner js-file-line">      {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC5" class="blob-code blob-code-inner js-file-line">          Console.Write($&quot;Learning {language} in a default way.&quot;);</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC6" class="blob-code blob-code-inner js-file-line">      }</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC7" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC8" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC9" class="blob-code blob-code-inner js-file-line">  interface IBackendDev : IDeveloper</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC10" class="blob-code blob-code-inner js-file-line">  {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC11" class="blob-code blob-code-inner js-file-line">      void LearnNewLanguage(string language)</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC12" class="blob-code blob-code-inner js-file-line">      {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC13" class="blob-code blob-code-inner js-file-line">          Console.Write($&quot;Learning {language} in a backend way.&quot;);</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC14" class="blob-code blob-code-inner js-file-line">      }</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC15" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC16" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC17" class="blob-code blob-code-inner js-file-line">  interface IFrontendDev : IDeveloper</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC18" class="blob-code blob-code-inner js-file-line">  {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC19" class="blob-code blob-code-inner js-file-line">      void LearnNewLanguage(string language)</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC20" class="blob-code blob-code-inner js-file-line">      {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC21" class="blob-code blob-code-inner js-file-line">          Console.Write($&quot;Learning {language} in a frontend way.&quot;);</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L22" class="blob-num js-line-number js-blob-rnum" data-line-number="22"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC22" class="blob-code blob-code-inner js-file-line">      }</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L23" class="blob-num js-line-number js-blob-rnum" data-line-number="23"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC23" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L24" class="blob-num js-line-number js-blob-rnum" data-line-number="24"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC24" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L25" class="blob-num js-line-number js-blob-rnum" data-line-number="25"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC25" class="blob-code blob-code-inner js-file-line">  interface IFullStackDev : IBackendDev, IFrontendDev { } // uses default implementation, </td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L26" class="blob-num js-line-number js-blob-rnum" data-line-number="26"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC26" class="blob-code blob-code-inner js-file-line">                                                          // of LearnNewLanguage, but which one?</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L27" class="blob-num js-line-number js-blob-rnum" data-line-number="27"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC27" class="blob-code blob-code-inner js-file-line">  class Dev : IFullStackDev { }</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L28" class="blob-num js-line-number js-blob-rnum" data-line-number="28"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC28" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L29" class="blob-num js-line-number js-blob-rnum" data-line-number="29"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC29" class="blob-code blob-code-inner js-file-line">  class Program</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L30" class="blob-num js-line-number js-blob-rnum" data-line-number="30"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC30" class="blob-code blob-code-inner js-file-line">  {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L31" class="blob-num js-line-number js-blob-rnum" data-line-number="31"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC31" class="blob-code blob-code-inner js-file-line">      static void Main(string[] args)</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L32" class="blob-num js-line-number js-blob-rnum" data-line-number="32"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC32" class="blob-code blob-code-inner js-file-line">      {</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L33" class="blob-num js-line-number js-blob-rnum" data-line-number="33"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC33" class="blob-code blob-code-inner js-file-line">          IFullStackDev dev = new Dev();</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L34" class="blob-num js-line-number js-blob-rnum" data-line-number="34"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC34" class="blob-code blob-code-inner js-file-line">          dev.LearnNewLanguage(&quot;TypeScript&quot;); // which method is called? The one</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L35" class="blob-num js-line-number js-blob-rnum" data-line-number="35"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC35" class="blob-code blob-code-inner js-file-line">                                              // from IBackendDev or IFrontendDev?</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L36" class="blob-num js-line-number js-blob-rnum" data-line-number="36"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC36" class="blob-code blob-code-inner js-file-line">      }</td>
        </tr>
        <tr>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-L37" class="blob-num js-line-number js-blob-rnum" data-line-number="37"></td>
          <td id="file-defaultinterfacemethod_diamondofdeath-cs-LC37" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/883f8f3b867572d9108ebe2048d62ee2/raw/39ad6f21746130f40cf0a48c17111032e0885335/DefaultInterfaceMethod_DiamondOfDeath.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/883f8f3b867572d9108ebe2048d62ee2#file-defaultinterfacemethod_diamondofdeath-cs" class="Link--inTextBlock">
          DefaultInterfaceMethod_DiamondOfDeath.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p><em>Please note that the syntax or places in which compiler generates errors may still change in the final feature&#8217;s version.</em></p>



<p>To better illustrate this Diamond of Death example, take a look at the following diagram:</p>



<figure class="wp-block-image"><img data-recalc-dims="1" decoding="async" width="801" height="332" data-attachment-id="3526" data-permalink="https://www.codejourney.net/csharp-8-default-interface-methods/diamondofdeath-2/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/DiamondOfDeath-1.jpg?fit=801%2C332&amp;ssl=1" data-orig-size="801,332" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="DiamondOfDeath" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/DiamondOfDeath-1.jpg?fit=801%2C332&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/DiamondOfDeath-1.jpg?resize=801%2C332&#038;ssl=1" alt="C# 8: Default Interface Methods Implementation: Diamond of Death example illustration" class="wp-image-3526" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/DiamondOfDeath-1.jpg?w=801&amp;ssl=1 801w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/DiamondOfDeath-1.jpg?resize=300%2C124&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/DiamondOfDeath-1.jpg?resize=768%2C318&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/DiamondOfDeath-1.jpg?resize=676%2C280&amp;ssl=1 676w" sizes="(max-width: 801px) 100vw, 801px" /><figcaption>Diamond of Death example illustration</figcaption></figure>



<p>The question is: <strong>which method should be called in such case?</strong></p>



<h4 class="wp-block-heading"><strong>Solution: most specific override rule</strong></h4>



<p>C# 8.0 will solve this issue by introducing <a rel="noreferrer noopener" aria-label="the most specific override rule (opens in a new tab)" href="https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md#the-most-specific-override-rule" target="_blank">the most specific override rule</a>. It means that in our case illustrated above, the <strong>compiler will issue an error and won&#8217;t let us do this</strong>.</p>


<p>To avoid this ambiguity, we&#8217;ll be forced to implement or override <span style="color: #ff6600;">LearnNewLanguage(string language)</span> in <span style="color: #ff6600;">IFullStackDev</span> interface. Only then it will be considered the most specific version of the method in the interfaces hierarchy we have. It will then be used when calling the method on objects contextually treated as instances of <span style="color: #ff6600;">IFullStackDev</span>. We could also implement it directly in the <span style="color: #ff6600;">Dev</span> class and use the class (not interface) type. It would then call the most specific method from the class. This way multiple inheritance issue will be eliminated.</p>


<h2 class="wp-block-heading">Compiler for the rescue</h2>



<p>There are a lot of other potential ambiguity scenarios that may take place after introducing into C# default interface methods. You can check all of them with potential solutions <a rel="noreferrer noopener" aria-label="are well-discussed on GitHub (opens in a new tab)" href="https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md" target="_blank">well-discussed on GitHub</a>.</p>



<p>The general assumption is that <strong>all code which may lead to issues related to default interface methods implementations is detected by the compiler</strong>. In effect, we should get appropriate errors when compiling the source code. That way, programmer can fix all potential conflicts directly when these arise.</p>



<h2 class="wp-block-heading">What about .NET Framework?</h2>



<p>What&#8217;s interesting with default interface methods is that <strong>it requres changes in the runtime</strong>. According to <a rel="noreferrer noopener" aria-label="current assumptions (opens in a new tab)" href="https://www.youtube.com/watch?v=HW_FnmnDlGQ&amp;feature=youtu.be&amp;t=3279" target="_blank">current assumptions</a>, it means that <strong>this feature will not work with .NET Framework</strong>! Only CoreCLR and Mono stack runtimes are going to receive these new updates.</p>



<h2 class="wp-block-heading">Summary</h2>



<p>As the motivation for introducing into C# default interface methods the language team members mention possibility to interact with some Android and iOS APIs, which already support such feature. They also claim that C# will get the <em><a rel="noreferrer noopener" aria-label="traits programming language feature (opens in a new tab)" href="https://en.wikipedia.org/wiki/Trait_(computer_programming)" target="_blank">traits</a></em><a rel="noreferrer noopener" aria-label="traits programming language feature (opens in a new tab)" href="https://en.wikipedia.org/wiki/Trait_(computer_programming)" target="_blank"> programming language feature</a> with default interface methods.</p>



<p>In my opinion this is the most controversial from all <a rel="noreferrer noopener" aria-label="C# 8.0 new features (opens in a new tab)" href="https://www.codejourney.net/tag/c8-0/" target="_blank">C# 8.0 new features</a>. It completely changes something which was obviousness for the developers for years. Probably we&#8217;ll see less breaking changes issues when the interfaces are extended, but <a rel="noreferrer noopener" aria-label="looking on the list of issues and edge cases it produces (opens in a new tab)" href="https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md" target="_blank">looking on the list of issues and edge cases it produces</a>&#8230; Maybe assuming it already works in a similar way in Java we should just accept it? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f914.png" alt="🤔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Another interesting, .NET-specific aspect is that this feature will not work with .NET Framework. It&#8217;s probably the very first moment when the difference in pace between .NET Core and Framework is so explicit.</p>



<p>I&#8217;m also very curious about the performance implications of default interface methods. I&#8217;ll for sure be following this issue on GitHub to see how it evolves <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p><strong><u class="remove-format">What do you think? Is it a good idea to introduce default interface methods into C#?</u></strong></p>
<p>The post <a href="https://www.codejourney.net/csharp-8-default-interface-methods/">C# 8: Default Interface Methods Implementation</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/csharp-8-default-interface-methods/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3495</post-id>	</item>
		<item>
		<title>C# 8: Slicing with Indexes and Ranges</title>
		<link>https://www.codejourney.net/csharp-8-slicing-indexes-ranges/</link>
					<comments>https://www.codejourney.net/csharp-8-slicing-indexes-ranges/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Wed, 13 Feb 2019 14:00:02 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C#8.0]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=3454</guid>

					<description><![CDATA[<p>C# 8.0 brings us another nice feature called slicing. In order to make it possible, two new concepts are introduced: Indexes and Ranges. Let&#8217;s see how this tiny feature is supposed to make our life easier 🙂 Goal of slicing The main purpose of introducing slicing into the language is to make working with arrays&#8230;</p>
<p>The post <a href="https://www.codejourney.net/csharp-8-slicing-indexes-ranges/">C# 8: Slicing with Indexes and Ranges</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>C# 8.0 brings us another nice feature called <strong>slicing</strong>. In order to make it possible, two new concepts are introduced: Indexes and Ranges.</p>



<p>Let&#8217;s see how this tiny feature is supposed to make our life easier <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-3454"></span>



<h2 class="wp-block-heading">Goal of slicing</h2>



<p>The main purpose of introducing slicing into the language is to make  working with arrays and strings easier.</p>



<p>Currently, when you want to retrieve a fragment of an array (X elements from the array, located for instance in its middle), you can use LINQ, i.e. combination of <a rel="noreferrer noopener" aria-label="Take (opens in a new tab)" href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.take?view=netframework-4.7.2" target="_blank">Take</a> and <a rel="noreferrer noopener" aria-label="Skip (opens in a new tab)" href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.skip?view=netframework-4.7.2" target="_blank">Skip</a> methods. You can also <a rel="noreferrer noopener" aria-label="create some extension method (opens in a new tab)" href="https://www.dotnetperls.com/array-slice" target="_blank">create some extension method</a> to provide kind of slicing.</p>



<p>To illustrate the current way, using LINQ to retrieve only 2nd, 3rd and 4th elements of an array looks as follows:</p>



<figure class="wp-block-embed"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist94580774" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-arrayslicewithlinq-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="ArraySliceWithLinq.cs content, created by dsibinski on 01:22PM on February 11, 2019."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="ArraySliceWithLinq.cs">
        <tr>
          <td id="file-arrayslicewithlinq-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-arrayslicewithlinq-cs-LC1" class="blob-code blob-code-inner js-file-line">  var array = new[] { &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39; , &#39;H&#39;};</td>
        </tr>
        <tr>
          <td id="file-arrayslicewithlinq-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-arrayslicewithlinq-cs-LC2" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-arrayslicewithlinq-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-arrayslicewithlinq-cs-LC3" class="blob-code blob-code-inner js-file-line">  var arraySlice = array.Skip(1).Take(3).ToArray(); // contains &#39;B&#39;, &#39;C&#39; and &#39;D&#39;</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/c13e6535c6601b033c5d6285a4908305/raw/916b574f79a9b821194d00f0c2fb999ca81ac3fa/ArraySliceWithLinq.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/c13e6535c6601b033c5d6285a4908305#file-arrayslicewithlinq-cs" class="Link--inTextBlock">
          ArraySliceWithLinq.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>C# community and designers decided it&#8217;s not convenient enough, so are <a rel="noreferrer noopener" aria-label="now adding&nbsp;to&nbsp;C#&nbsp;indexes&nbsp;and&nbsp;ranges&nbsp;(in&nbsp;order to provide slicing) (opens in a new tab)" href="https://github.com/dotnet/csharplang/issues/185" target="_blank">now adding&nbsp;to&nbsp;C#&nbsp;8 indexes&nbsp;and&nbsp;ranges&nbsp;(in&nbsp;order to provide slicing)</a> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">Index</h2>


<p>C# 8.0 comes along with a new object &#8211; <span style="color: #ff6600;">System.Index</span>. It&#8217;s a structure internally and looks as follows:</p>


<figure class="wp-block-image"><img data-recalc-dims="1" decoding="async" width="1016" height="486" data-attachment-id="3460" data-permalink="https://www.codejourney.net/csharp-8-slicing-indexes-ranges/indexstruct/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/IndexStruct.png?fit=1016%2C486&amp;ssl=1" data-orig-size="1016,486" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="IndexStruct" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/IndexStruct.png?fit=1016%2C486&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/IndexStruct.png?resize=1016%2C486&#038;ssl=1" alt="C# 8 indexes and ranges: System.Index structure - .NET Core 3.0.0-preview-27324-5" class="wp-image-3460" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/IndexStruct.png?w=1016&amp;ssl=1 1016w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/IndexStruct.png?resize=300%2C144&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/IndexStruct.png?resize=768%2C367&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/IndexStruct.png?resize=676%2C323&amp;ssl=1 676w" sizes="(max-width: 1016px) 100vw, 1016px" /><figcaption>System.Index <em>s</em>tructure &#8211; .NET Core 3.0.0-preview-27324-5</figcaption></figure>


<p>What&#8217;s interesting here, is that the constructor takes the <span style="color: #ff6600;">value</span> of the <span style="color: #000000;">index </span>and boolean flag <span style="color: #ff6600;">fromEnd</span>. Let&#8217;s see how we can use it in practice:</p>


<figure class="wp-block-embed"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist94584228" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-indexescsharp8-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="IndexesCsharp8.cs content, created by dsibinski on 04:11PM on February 11, 2019."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="IndexesCsharp8.cs">
        <tr>
          <td id="file-indexescsharp8-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-indexescsharp8-cs-LC1" class="blob-code blob-code-inner js-file-line">  var array = new[] { &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39; };</td>
        </tr>
        <tr>
          <td id="file-indexescsharp8-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-indexescsharp8-cs-LC2" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-indexescsharp8-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-indexescsharp8-cs-LC3" class="blob-code blob-code-inner js-file-line">  Index idx1 = 2;  // 2nd element from the beginning</td>
        </tr>
        <tr>
          <td id="file-indexescsharp8-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-indexescsharp8-cs-LC4" class="blob-code blob-code-inner js-file-line">  Index idx2 = ^3; // 3rd element from the end (^)</td>
        </tr>
        <tr>
          <td id="file-indexescsharp8-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-indexescsharp8-cs-LC5" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-indexescsharp8-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-indexescsharp8-cs-LC6" class="blob-code blob-code-inner js-file-line">  Console.WriteLine($&quot;{array[idx1]}, {array[idx2]}&quot;); // prints &quot;C, F&quot;</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/798ba2d501ecdbca5c4fe29e5c4ff5a3/raw/32ecae5f14941b2f4ee01ad5d47cf76a05d06739/IndexesCsharp8.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/798ba2d501ecdbca5c4fe29e5c4ff5a3#file-indexescsharp8-cs" class="Link--inTextBlock">
          IndexesCsharp8.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>While the first printed value is nothing special, as you can see we can now use the <strong>hat operator</strong> (^) in order to <strong>index from the end</strong>. The following also works:</p>



<figure class="wp-block-embed"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist94584292" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-indexes_ccharp8_2-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Indexes_CCharp8_2.cs content, created by dsibinski on 04:14PM on February 11, 2019."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Indexes_CCharp8_2.cs">
        <tr>
          <td id="file-indexes_ccharp8_2-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-indexes_ccharp8_2-cs-LC1" class="blob-code blob-code-inner js-file-line">  var array = new[] { &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39; };</td>
        </tr>
        <tr>
          <td id="file-indexes_ccharp8_2-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-indexes_ccharp8_2-cs-LC2" class="blob-code blob-code-inner js-file-line">  Console.WriteLine($&quot;{array[^3]}&quot;); // prints &quot;F&quot; (3rd element from the end)</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/1065a08b53aa7b0e5eb9ca19a3285f19/raw/3af3373353f98f3e0e9ae9d47db0168af5251dcf/Indexes_CCharp8_2.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/1065a08b53aa7b0e5eb9ca19a3285f19#file-indexes_ccharp8_2-cs" class="Link--inTextBlock">
          Indexes_CCharp8_2.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>


<p>It means that the ^ operator is a syntactic sugar for<span style="color: #ff6600;"> Index</span>. I allowed myself to check the IL produced:</p>


<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="824" height="376" data-attachment-id="3461" data-permalink="https://www.codejourney.net/csharp-8-slicing-indexes-ranges/indexctorsyntacticsugerhat/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/IndexCtorSyntacticSugerHat.png?fit=824%2C376&amp;ssl=1" data-orig-size="824,376" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="IndexCtorSyntacticSugerHat" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/IndexCtorSyntacticSugerHat.png?fit=824%2C376&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/IndexCtorSyntacticSugerHat.png?resize=824%2C376&#038;ssl=1" alt="C# 8 indexes and ranges: IL Code behind ^ (hat operator)" class="wp-image-3461" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/IndexCtorSyntacticSugerHat.png?w=824&amp;ssl=1 824w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/IndexCtorSyntacticSugerHat.png?resize=300%2C137&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/IndexCtorSyntacticSugerHat.png?resize=768%2C350&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/IndexCtorSyntacticSugerHat.png?resize=676%2C308&amp;ssl=1 676w" sizes="auto, (max-width: 824px) 100vw, 824px" /><figcaption>IL Code behind ^ (hat operator)</figcaption></figure>


<p>As we suspected &#8211; an instance of <span style="color: #ff6600;">Index</span> is created, passing <strong>3</strong> as <span style="color: #ff6600;">value</span> and <strong>true</strong> as <span style="color: #ff6600;">fromEnd</span>&nbsp;(see the constructor parameters above).</p>


<h2 class="wp-block-heading">Range</h2>


<p>Another concept which uses Indexes is a new structural data type &#8211; <span style="color: #ff6600;">System.Range</span>:</p>


<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1012" height="478" data-attachment-id="3463" data-permalink="https://www.codejourney.net/csharp-8-slicing-indexes-ranges/rangeusingindexes/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/RangeUsingIndexes.png?fit=1012%2C478&amp;ssl=1" data-orig-size="1012,478" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="RangeUsingIndexes" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/RangeUsingIndexes.png?fit=1012%2C478&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/RangeUsingIndexes.png?resize=1012%2C478&#038;ssl=1" alt="C# 8 indexes and ranges: System.Range structure - .NET Core 3.0.0-preview-27324-5" class="wp-image-3463" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/RangeUsingIndexes.png?w=1012&amp;ssl=1 1012w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/RangeUsingIndexes.png?resize=300%2C142&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/RangeUsingIndexes.png?resize=768%2C363&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/RangeUsingIndexes.png?resize=676%2C319&amp;ssl=1 676w" sizes="auto, (max-width: 1012px) 100vw, 1012px" /><figcaption> <em>System.Range structure &#8211; .NET Core 3.0.0-preview-27324-5</em> </figcaption></figure>



<p>As you can see, it <strong>really uses Indexes</strong>. We can even try creating it on our own:</p>



<figure class="wp-block-embed"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist94584738" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-rangecsharp8-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="RangeCSharp8.cs content, created by dsibinski on 04:35PM on February 11, 2019."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="RangeCSharp8.cs">
        <tr>
          <td id="file-rangecsharp8-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-rangecsharp8-cs-LC1" class="blob-code blob-code-inner js-file-line">  var array = new[] { &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39; };</td>
        </tr>
        <tr>
          <td id="file-rangecsharp8-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-rangecsharp8-cs-LC2" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-rangecsharp8-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-rangecsharp8-cs-LC3" class="blob-code blob-code-inner js-file-line">  Range r = Range.Create(2, ^1); // range from 2nd to </td>
        </tr>
        <tr>
          <td id="file-rangecsharp8-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-rangecsharp8-cs-LC4" class="blob-code blob-code-inner js-file-line">                                 // 1st element from the end</td>
        </tr>
        <tr>
          <td id="file-rangecsharp8-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-rangecsharp8-cs-LC5" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-rangecsharp8-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-rangecsharp8-cs-LC6" class="blob-code blob-code-inner js-file-line">  Console.WriteLine(array[r]); // prints &quot;CDEFG&quot;</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/6621a3ca282c539274851b4bba11c8cf/raw/57acffc675f86be754fef1a451198fa2480ab2a4/RangeCSharp8.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/6621a3ca282c539274851b4bba11c8cf#file-rangecsharp8-cs" class="Link--inTextBlock">
          RangeCSharp8.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>


<p>This doesn&#8217;t really make extracting data from the arrays better than <em>Skip-Take</em> LINQ, does it?</p>
<p>Fortunately, there&#8217;s another friendly C# language element introduced called <em>range expression</em>. It can be written as <span style="color: #ff6600;">x..y</span> and used directly for indexing:</p>
<p><style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist94584820" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-rangeexpression-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="RangeExpression.cs content, created by dsibinski on 04:39PM on February 11, 2019."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="RangeExpression.cs">
        <tr>
          <td id="file-rangeexpression-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-rangeexpression-cs-LC1" class="blob-code blob-code-inner js-file-line">  var array = new[] { &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39; };</td>
        </tr>
        <tr>
          <td id="file-rangeexpression-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-rangeexpression-cs-LC2" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-rangeexpression-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-rangeexpression-cs-LC3" class="blob-code blob-code-inner js-file-line">  Console.WriteLine(array[2..^1]); // prints &quot;CDEFG&quot;</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/28e64583ab06c323d1154548b48a271e/raw/fb2cd5b0d63f54247e3bc0d8322c7aa88d81eb21/RangeExpression.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/28e64583ab06c323d1154548b48a271e#file-rangeexpression-cs" class="Link--inTextBlock">
          RangeExpression.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>
</p>
<p>This looks much better! Let&#8217;s see how C# compiler translated our 2 lines of code by looking at the pseudo-C# IL produced:</p>


<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="504" height="640" data-attachment-id="3464" data-permalink="https://www.codejourney.net/csharp-8-slicing-indexes-ranges/rangearrayilcsharp/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/RangeArrayILCSharp.png?fit=504%2C640&amp;ssl=1" data-orig-size="504,640" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="RangeArrayILCSharp" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/RangeArrayILCSharp.png?fit=504%2C640&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/RangeArrayILCSharp.png?resize=504%2C640&#038;ssl=1" alt="C# 8 indexes and ranges: IL (C# pseudo-code) produced when using range expression" class="wp-image-3464" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/RangeArrayILCSharp.png?w=504&amp;ssl=1 504w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/RangeArrayILCSharp.png?resize=236%2C300&amp;ssl=1 236w" sizes="auto, (max-width: 504px) 100vw, 504px" /><figcaption>IL (C# pseudo-code) produced when using range expression</figcaption></figure>



<p>Even though we could achieve the same with much less LINQ, C# compiler does a lot of stuff for us, keeping our code simpler to grasp.</p>



<h4 class="wp-block-heading"><strong>Ranges in strings</strong></h4>


<p>All these operations apply also to <span style="color: #ff6600;">strings</span>:</p>
<p><style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist94585049" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-csharp8_rangestring-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="CSharp8_RangeString.cs content, created by dsibinski on 04:49PM on February 11, 2019."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="CSharp8_RangeString.cs">
        <tr>
          <td id="file-csharp8_rangestring-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-csharp8_rangestring-cs-LC1" class="blob-code blob-code-inner js-file-line">  var story = &quot;C# 8 is going to be great!&quot;;</td>
        </tr>
        <tr>
          <td id="file-csharp8_rangestring-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-csharp8_rangestring-cs-LC2" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-csharp8_rangestring-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-csharp8_rangestring-cs-LC3" class="blob-code blob-code-inner js-file-line">  Console.WriteLine(story[^6..^0]); // prints &quot;great!&quot; (last 6 chars)</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/6f011df284ea03178178cceed6f902b4/raw/1c33e97219a57324005049b9f182916934932784/CSharp8_RangeString.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/6f011df284ea03178178cceed6f902b4#file-csharp8_rangestring-cs" class="Link--inTextBlock">
          CSharp8_RangeString.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>
</p>
<p></p>


<h4 class="wp-block-heading"><strong>Ranges in foreach loop</strong></h4>


<p>Next interesting usage is in the <span style="color: #ff6600;">foreach</span> loop:</p>
<p><style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist94585228" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-csharp8_rangeforeach-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="CSharp8_RangeForeach.cs content, created by dsibinski on 05:00PM on February 11, 2019."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="CSharp8_RangeForeach.cs">
        <tr>
          <td id="file-csharp8_rangeforeach-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-csharp8_rangeforeach-cs-LC1" class="blob-code blob-code-inner js-file-line">  var devs = new Developer[]</td>
        </tr>
        <tr>
          <td id="file-csharp8_rangeforeach-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-csharp8_rangeforeach-cs-LC2" class="blob-code blob-code-inner js-file-line">        {</td>
        </tr>
        <tr>
          <td id="file-csharp8_rangeforeach-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-csharp8_rangeforeach-cs-LC3" class="blob-code blob-code-inner js-file-line">            new Developer(&quot;Dawid&quot;),</td>
        </tr>
        <tr>
          <td id="file-csharp8_rangeforeach-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-csharp8_rangeforeach-cs-LC4" class="blob-code blob-code-inner js-file-line">            new Developer(&quot;Mark&quot;),</td>
        </tr>
        <tr>
          <td id="file-csharp8_rangeforeach-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-csharp8_rangeforeach-cs-LC5" class="blob-code blob-code-inner js-file-line">            new Developer(&quot;John&quot;),</td>
        </tr>
        <tr>
          <td id="file-csharp8_rangeforeach-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-csharp8_rangeforeach-cs-LC6" class="blob-code blob-code-inner js-file-line">            new Developer(&quot;Alice&quot;),</td>
        </tr>
        <tr>
          <td id="file-csharp8_rangeforeach-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-csharp8_rangeforeach-cs-LC7" class="blob-code blob-code-inner js-file-line">            new Developer(&quot;Kate&quot;)</td>
        </tr>
        <tr>
          <td id="file-csharp8_rangeforeach-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-csharp8_rangeforeach-cs-LC8" class="blob-code blob-code-inner js-file-line">        };</td>
        </tr>
        <tr>
          <td id="file-csharp8_rangeforeach-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-csharp8_rangeforeach-cs-LC9" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-csharp8_rangeforeach-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-csharp8_rangeforeach-cs-LC10" class="blob-code blob-code-inner js-file-line">  foreach (var dev in devs[1..^2]) // prints &quot;MarkJohn&quot;</td>
        </tr>
        <tr>
          <td id="file-csharp8_rangeforeach-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-csharp8_rangeforeach-cs-LC11" class="blob-code blob-code-inner js-file-line">  {</td>
        </tr>
        <tr>
          <td id="file-csharp8_rangeforeach-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-csharp8_rangeforeach-cs-LC12" class="blob-code blob-code-inner js-file-line">      Console.Write(dev.FirstName);</td>
        </tr>
        <tr>
          <td id="file-csharp8_rangeforeach-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-csharp8_rangeforeach-cs-LC13" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/92b499a8c0567c88001f7338d149c519/raw/529e6f6428932ace2f5a2932beca25f33947f88b/CSharp8_RangeForeach.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/92b499a8c0567c88001f7338d149c519#file-csharp8_rangeforeach-cs" class="Link--inTextBlock">
          CSharp8_RangeForeach.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>
</p>


<h2 class="wp-block-heading">Summary</h2>



<p>I see these &#8220;little&#8221; C# 8 indexes and ranges as another helpful feature. It&#8217;s less breaking and controversial than <a rel="noreferrer noopener" aria-label="nullable reference types (opens in a new tab)" href="https://www.codejourney.net/2019/02/csharp-8-nullable-reference-types/" target="_blank">nullable reference types</a>, still making us write less C# code in the future.</p>



<p>If you&#8217;d like to dig the feature even more, I encourage you to read the <a rel="noreferrer noopener" aria-label="discussions about it on GItHub, (opens in a new tab)" href="https://github.com/dotnet/csharplang/issues/198" target="_blank">discussions about it on GitHub,</a> which I always find very interesting <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Developers, what do you think about C# 8 indexes and ranges? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f914.png" alt="🤔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The post <a href="https://www.codejourney.net/csharp-8-slicing-indexes-ranges/">C# 8: Slicing with Indexes and Ranges</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/csharp-8-slicing-indexes-ranges/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3454</post-id>	</item>
		<item>
		<title>C# 8: Nullable Reference Types</title>
		<link>https://www.codejourney.net/csharp-8-nullable-reference-types/</link>
					<comments>https://www.codejourney.net/csharp-8-nullable-reference-types/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Tue, 05 Feb 2019 14:00:14 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C#8.0]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=3406</guid>

					<description><![CDATA[<p>In the few next posts I&#8217;d like to share with you some of the most interesting C# 8.0 features. Today we&#8217;re going to start with examining nullable reference types. Let&#8217;s see then 🙂 Learn about building C# 8.0 C# 8.0 is going to be the next major release of C# language. It should be released&#8230;</p>
<p>The post <a href="https://www.codejourney.net/csharp-8-nullable-reference-types/">C# 8: Nullable Reference Types</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In the few next posts I&#8217;d like to share with you some of the most interesting C# 8.0 features. Today we&#8217;re going to start with examining nullable reference types. Let&#8217;s see then <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>


<span id="more-3406"></span>



<h2 class="wp-block-heading">Learn about building C# 8.0</h2>


<p>C# 8.0 is going to be the next major release of C# language. It should be released by the same time as .NET Core 3.0.</p>
<p>What I find very interesting is <a href="https://github.com/dotnet/csharplang" target="_blank" rel="noopener">C# language GitHub repository</a>. Among others, you can find there very interesting <a href="https://github.com/dotnet/csharplang/tree/master/meetings" target="_blank" rel="noopener">notes from C# language design meetings</a> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> If you filter well, you can also list so called &#8220;proposals&#8221;, which are current candidates for new C#&#8217;s version (or sub-versions) features. By clicking <a href="https://github.com/dotnet/csharplang/issues?q=is%3Aissue+milestone%3A%228.0+candidate%22+is%3Aopen" target="_blank" rel="noopener">here</a> you can list C# 8.0 candidates.</p>
<p>I will publish a separate post regarding each feature which I find particularly interesting <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>


<h2 class="wp-block-heading">Nullable reference types</h2>



<p>First of the C# 8.0 features I&#8217;d like to discuss are <a rel="noreferrer noopener" aria-label="nullable reference types (opens in a new tab)" href="https://github.com/dotnet/csharplang/issues/36" target="_blank">nullable reference types</a>. It seems it&#8217;s going to change the way we write our code, so it&#8217;s worth knowing this feature.</p>



<h3 class="wp-block-heading">A bit of history &#8211; NullReferenceException</h3>


<p>Consider the following code:</p>


<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="586" height="679" data-attachment-id="3422" data-permalink="https://www.codejourney.net/csharp-8-nullable-reference-types/nullrefcode/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullRefCode.png?fit=586%2C679&amp;ssl=1" data-orig-size="586,679" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="NullRefCode" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullRefCode.png?fit=586%2C679&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullRefCode.png?resize=586%2C679&#038;ssl=1" alt="" class="wp-image-3422" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullRefCode.png?w=586&amp;ssl=1 586w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullRefCode.png?resize=259%2C300&amp;ssl=1 259w" sizes="auto, (max-width: 586px) 100vw, 586px" /><figcaption>Gist: <a href="https://gist.github.com/dsibinski/c1c9fabb9f05c1e1fed6a58278dce7e5" target="_blank" rel="noreferrer noopener" aria-label="NullCode.cs (opens in a new tab)">NullCode.cs</a></figcaption></figure>


<p>Guess what happens after executing this as a console app? Of course, our favorite <span style="color: #ff6600;">NullReferenceException</span> is thrown:</p>


<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="199" data-attachment-id="3423" data-permalink="https://www.codejourney.net/csharp-8-nullable-reference-types/nullreferenceexception-2/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullReferenceException-1.png?fit=1109%2C216&amp;ssl=1" data-orig-size="1109,216" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="NullReferenceException" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullReferenceException-1.png?fit=1024%2C199&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullReferenceException-1.png?resize=1024%2C199&#038;ssl=1" alt="" class="wp-image-3423" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullReferenceException-1.png?resize=1024%2C199&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullReferenceException-1.png?resize=300%2C58&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullReferenceException-1.png?resize=768%2C150&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullReferenceException-1.png?resize=676%2C132&amp;ssl=1 676w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullReferenceException-1.png?w=1109&amp;ssl=1 1109w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>


<p>In this case, <span style="color: #ff6600;">jsDev.LastName</span> is of course set to <span style="color: #ff6600;">null</span>.</p>
<p><span style="color: #ff6600;">NullReferenceException</span> has been a nightmare for thousands of developers (and users) for many years. What if, in the cases like our example, we knew before that in this place our object might be <span style="color: #ff6600;">null</span>?</p>


<h3 class="wp-block-heading">New world &#8211; #nullable enable</h3>


<p>For that purpose, C# development team is introducing <strong>nullable reference types</strong>. In essence, it<strong> makes reference types (like <span style="color: #ff6600;">string</span>) non-nullable</strong>! However to not be a tough nut to crack, all issues related to assigning <span style="color: #ff6600;">null</span> value to non-nullable reference types are presented as warnings, not errors.</p>


<p>In order to use this feature, you need to enable it.  You can do it with a preprocessor directive <em>#nullable enabled.</em> We can do it directly in the source code file. In the future, it <a rel="noreferrer noopener" aria-label="should also be possible (opens in a new tab)" href="https://github.com/dotnet/project-system/issues/4058" target="_blank">should also be possible</a> to enable it on project level.</p>



<p>Notice what happens when we enable it in our example:</p>



<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="616" height="405" data-attachment-id="3425" data-permalink="https://www.codejourney.net/csharp-8-nullable-reference-types/propertynotinitializedwarning/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PropertyNotInitializedWarning.png?fit=616%2C405&amp;ssl=1" data-orig-size="616,405" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="PropertyNotInitializedWarning" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PropertyNotInitializedWarning.png?fit=616%2C405&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PropertyNotInitializedWarning.png?resize=616%2C405&#038;ssl=1" alt="" class="wp-image-3425" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PropertyNotInitializedWarning.png?w=616&amp;ssl=1 616w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PropertyNotInitializedWarning.png?resize=300%2C197&amp;ssl=1 300w" sizes="auto, (max-width: 616px) 100vw, 616px" /></figure>


<p>We&#8217;re still able to compile, but this nice warning is displayed <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> As soon as we initialize <span style="color: #ff6600;">LastName</span> property in the constructor, the warning disappears:</p>


<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="656" height="405" data-attachment-id="3426" data-permalink="https://www.codejourney.net/csharp-8-nullable-reference-types/propertynotinitializedwarningdisappeared/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PropertyNotInitializedWarningDisappeared.png?fit=656%2C405&amp;ssl=1" data-orig-size="656,405" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="PropertyNotInitializedWarningDisappeared" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PropertyNotInitializedWarningDisappeared.png?fit=656%2C405&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PropertyNotInitializedWarningDisappeared.png?resize=656%2C405&#038;ssl=1" alt="" class="wp-image-3426" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PropertyNotInitializedWarningDisappeared.png?w=656&amp;ssl=1 656w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PropertyNotInitializedWarningDisappeared.png?resize=300%2C185&amp;ssl=1 300w" sizes="auto, (max-width: 656px) 100vw, 656px" /></figure>



<p>If you try to cheat, it still works like a charm <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> :</p>



<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="870" height="418" data-attachment-id="3427" data-permalink="https://www.codejourney.net/csharp-8-nullable-reference-types/nullassignationnonnullablestring/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullAssignationNonnullableString.png?fit=870%2C418&amp;ssl=1" data-orig-size="870,418" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="NullAssignationNonnullableString" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullAssignationNonnullableString.png?fit=870%2C418&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullAssignationNonnullableString.png?resize=870%2C418&#038;ssl=1" alt="" class="wp-image-3427" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullAssignationNonnullableString.png?w=870&amp;ssl=1 870w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullAssignationNonnullableString.png?resize=300%2C144&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullAssignationNonnullableString.png?resize=768%2C369&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullAssignationNonnullableString.png?resize=676%2C325&amp;ssl=1 676w" sizes="auto, (max-width: 870px) 100vw, 870px" /></figure>


<p>But hey&#8230; what if you actually want <span style="color: #ff6600;">null</span> in this particular <span style="color: #ff6600;">string</span>? <strong>You need to explicitly mark your reference type variable as nullable</strong>. You probably recall nullable value types like <span style="color: #ff6600;">int?</span>, don&#8217;t you (read <a href="https://www.codejourney.net/2018/08/net-internals-02-stack-and-heap-net-data-structures/" target="_blank" rel="noopener">this post</a> if you don&#8217;t remember the difference between value and reference types)? Now you can do the same with non-nullable reference types:</p>


<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="491" height="342" data-attachment-id="3428" data-permalink="https://www.codejourney.net/csharp-8-nullable-reference-types/nullablereferencestring/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullableReferenceString.png?fit=491%2C342&amp;ssl=1" data-orig-size="491,342" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="NullableReferenceString" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullableReferenceString.png?fit=491%2C342&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullableReferenceString.png?resize=491%2C342&#038;ssl=1" alt="" class="wp-image-3428" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullableReferenceString.png?w=491&amp;ssl=1 491w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullableReferenceString.png?resize=300%2C209&amp;ssl=1 300w" sizes="auto, (max-width: 491px) 100vw, 491px" /></figure>



<p>Nonetheless, in such case, the compiler still takes care of us:</p>



<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="774" height="210" data-attachment-id="3429" data-permalink="https://www.codejourney.net/csharp-8-nullable-reference-types/possiblenullwarning/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PossibleNullWarning.png?fit=774%2C210&amp;ssl=1" data-orig-size="774,210" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="PossibleNullWarning" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PossibleNullWarning.png?fit=774%2C210&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PossibleNullWarning.png?resize=774%2C210&#038;ssl=1" alt="" class="wp-image-3429" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PossibleNullWarning.png?w=774&amp;ssl=1 774w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PossibleNullWarning.png?resize=300%2C81&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PossibleNullWarning.png?resize=768%2C208&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/PossibleNullWarning.png?resize=676%2C183&amp;ssl=1 676w" sizes="auto, (max-width: 774px) 100vw, 774px" /></figure>


<p>The final fix is quite obvious &#8211; we need to check for <span style="color: #ff6600;">null</span> first:</p>


<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="691" height="195" data-attachment-id="3430" data-permalink="https://www.codejourney.net/csharp-8-nullable-reference-types/checkingfornull/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/CheckingForNull.png?fit=691%2C195&amp;ssl=1" data-orig-size="691,195" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="CheckingForNull" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/CheckingForNull.png?fit=691%2C195&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/CheckingForNull.png?resize=691%2C195&#038;ssl=1" alt="" class="wp-image-3430" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/CheckingForNull.png?w=691&amp;ssl=1 691w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/CheckingForNull.png?resize=300%2C85&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/CheckingForNull.png?resize=676%2C191&amp;ssl=1 676w" sizes="auto, (max-width: 691px) 100vw, 691px" /></figure>



<p>At this stage all warnings finally disappear. You can find the finished source code <a rel="noreferrer noopener" aria-label="here (opens in a new tab)" href="https://gist.github.com/dsibinski/67c6d315463186bbfa4281dbb6611a25" target="_blank">here</a>.</p>



<h3 class="wp-block-heading">Nullable reference types in IL</h3>



<p>The only thing I found in the IL code compiled from source code with <em>#nullable enabled</em> directive was NullableAttribute applied to properties:</p>



<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="878" height="328" data-attachment-id="3440" data-permalink="https://www.codejourney.net/csharp-8-nullable-reference-types/nullablereftypesil/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullableRefTypesIL.png?fit=878%2C328&amp;ssl=1" data-orig-size="878,328" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="NullableRefTypesIL" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullableRefTypesIL.png?fit=878%2C328&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullableRefTypesIL.png?resize=878%2C328&#038;ssl=1" alt="" class="wp-image-3440" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullableRefTypesIL.png?w=878&amp;ssl=1 878w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullableRefTypesIL.png?resize=300%2C112&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullableRefTypesIL.png?resize=768%2C287&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2019/02/NullableRefTypesIL.png?resize=676%2C253&amp;ssl=1 676w" sizes="auto, (max-width: 878px) 100vw, 878px" /></figure>



<p>It seems that nullable reference types are implemented by applying this attribute to variables, which is only used by the compiler (runtime is not affected).</p>



<h2 class="wp-block-heading">Summary</h2>



<p>I&#8217;m really excited about nullable reference types in C# 8. I personally feel that it even introduces some clarity in the language &#8211; we&#8217;ll have explicit declarations for nullable value and reference types. However, in more complex scenarios it may bring some confusion in regards to reference types and using &#8220;?&#8221; character (like generics or collections &#8211; see <a href="https://codeblog.jonskeet.uk/2018/04/21/first-steps-with-nullable-reference-types/" target="_blank" rel="noreferrer noopener" aria-label="Jon Skeet's post (opens in a new tab)">Jon Skeet&#8217;s post</a> for more details). For sure it&#8217;s a significant step towards eliminating issues with exceptions caused by null references.</p>



<p>What do you think about nullable reference types? Is it a good direction in which C# language is going? Share your thoughts in the comments! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The post <a href="https://www.codejourney.net/csharp-8-nullable-reference-types/">C# 8: Nullable Reference Types</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/csharp-8-nullable-reference-types/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3406</post-id>	</item>
	</channel>
</rss>
