{"id":28816,"date":"2020-02-19T23:45:00","date_gmt":"2020-02-19T22:45:00","guid":{"rendered":"https:\/\/blexin.com\/le-novita-di-c-8-seconda-parte\/"},"modified":"2021-05-20T18:40:37","modified_gmt":"2021-05-20T16:40:37","slug":"new-features-of-c-8-part-two","status":"publish","type":"post","link":"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/","title":{"rendered":"New features of C# 8: part two"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"608\" data-attachment-id=\"27386\" data-permalink=\"https:\/\/blexin.com\/en\/image00-7-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image00-7.png?fit=1024%2C608&amp;ssl=1\" data-orig-size=\"1024,608\" data-comments-opened=\"0\" 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=\"image00\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image00-7.png?fit=1024%2C608&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image00-7.png?resize=1024%2C608&#038;ssl=1\" alt=\"\" class=\"wp-image-27386\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image00-7.png 1024w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image00-7-980x582.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image00-7-480x285.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In my previous article, we discover the first part of the new features of C#8: let\u2019s see now together what else this new version of Microsoft\u2019s home language makes available to us.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>New Switch Expression<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The new switch expressions have been released, and they introduce a compact method for writing switch\/case expressions.<br>I show you with an example of what I am talking about:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic static string GetWaterCondition(WaterCondition condition) =&gt;\ncondition.Sea switch\n{\n\u00a0\u00a0\u00a0\u00a0Sea.Calm =&gt; &quot;The sea is calm&quot;,\n\u00a0\u00a0\u00a0\u00a0Sea.Rough =&gt; &quot;The sea is rough&quot;,\n\u00a0\u00a0\u00a0\u00a0Sea.VRough =&gt; &quot;The sea is very rough&quot;,\n\u00a0\u00a0\u00a0\u00a0_ =&gt; &quot;Undefined&quot;\n};\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">As you can note:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>the variable foreruns the keyword&nbsp;<em>switch<\/em>&nbsp;<\/li><li>The \u201c=&gt;\u201d substitutes the elements \u201ccase\u201d and \u201c:\u201d<\/li><li>An underscore (or discard) \u201c_\u201d substitutes the default case<\/li><li>Bodies are expressions and not instructions&nbsp;<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Properties criteria<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks to the previous syntax, we can use the property matching too. We can indicate properties themselves in the conditions, instead of specifying the object to evaluate:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic static string GetWaterCondition(WaterCondition condition) =&gt; condition switch\n{\n\u00a0\u00a0\u00a0\u00a0{Sea: Sea.Calm} =&gt; &quot;The sea is calm&quot;,\n\u00a0\u00a0\u00a0\u00a0{Sea: Sea.Rough} =&gt; &quot;The sea is rough&quot;,\n\u00a0\u00a0\u00a0\u00a0{Sea: Sea.VRough, Wind: Wind.Strong} =&gt; &quot;There\u2019s a storm!&quot;,\n\u00a0\u00a0\u00a0\u00a0{Sea: Sea.VRough} =&gt; &quot;The sea is very rough&quot;,\n\u00a0\u00a0\u00a0\u00a0null =&gt; \u201cUnknown\u201d,\n\u00a0\u00a0\u00a0\u00a0_ =&gt; &quot;Undefined&quot;\n};\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We evaluate the property&nbsp;<em>Sea<\/em>&nbsp;as before, but we can evaluate the property&nbsp;<em>Wind<\/em>&nbsp;too, and, in addition to the default value (\u201c_\u201d), we can check if the value is null too.Please pay attention to the evaluation order: in the previous code snippet, we need to evaluate the much specific condition with a&nbsp;<em>very rough sea and strong wind<\/em>&nbsp;(<code>There\u2019s a storm!<\/code>) and then pass to the condition of a<em>&nbsp;very rough sea<\/em>&nbsp;(<code>The sea is very rough<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Criteria for tuples<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It is possible to use the&nbsp;<em>tuple&nbsp;<\/em>(<em>tuple pattern<\/em>), which are similar to the property pattern, as&nbsp;<em>switch<\/em>&nbsp;argument:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic string Location(string country, string city)\n\u00a0\u00a0\u00a0=&gt; (country, city) switch\n\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(&quot;IT&quot;, &quot;Naples&quot;) =&gt; &quot;Naples, Italy (Campania)\u201d,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(_, &quot;Naples&quot;) =&gt; &quot;Italy (Campania),\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0United States (Utah),\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0United States (Texas),\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0United States (Florida),\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0United States (New York)&quot;,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(&quot;IT&quot;, _) =&gt; &quot;Italy&quot;,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0_ =&gt; &quot;Not Found&quot;, \/\/Short form for ( _, _) =&gt; &quot;Not Found&quot;\n\u00a0\u00a0\u00a0};\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Positioning criteria<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Some objects directly or indirectly implement the&nbsp;<em>deconstruct<\/em>&nbsp;introduced with C#7, that \u201csmooths over\u201d an object in a parameters list.<br>Using the&nbsp;<em>deconstruct<\/em>&nbsp;and the&nbsp;<em>when<\/em>&nbsp;clause in a&nbsp;<em>switch<\/em>&nbsp;instruction, we can use the&nbsp;<em>positional pattern<\/em>. In this example, we can see how easy it is to map a point in a Cartesian plane to see in which quadrant it is placed.<br>Supposing, we have this type of implementation:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Point\n{\n\u00a0\u00a0\u00a0\u00a0public int X { get; }\n\u00a0\u00a0\u00a0\u00a0public int Y { get; }\n\u00a0\n\u00a0\u00a0\u00a0\u00a0public Point(int x, int y) =&gt; (X, Y) = (x, y);\n\u00a0\n\u00a0\u00a0\u00a0\u00a0public void Deconstruct(out int x, out int y) =&gt; (x, y) = (X, Y);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The object Point is deconstructed and inserted in a tuple; then we use the then clause to determine in which quadrant is the point:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nprivate string GetPointPosition(Point point) =&gt; point switch\n{\n\u00a0\u00a0\u00a0\u00a0(0, 0) =&gt; &quot;origin&quot;,\n\u00a0\u00a0\u00a0\u00a0var (x, y) when x &gt; 0 &amp;&amp; y &gt; 0 =&gt; &quot;dx-up&quot;,\n\u00a0\u00a0\u00a0\u00a0var (x, y) when x &lt; 0 &amp;&amp; y &gt; 0 =&gt; &quot;sx-up&quot;,\n\u00a0\u00a0\u00a0\u00a0var (x, y) when x &lt; 0 &amp;&amp; y &lt; 0 =&gt; &quot;sx-down&quot;,\n\u00a0\u00a0\u00a0\u00a0var (x, y) when x &gt; 0 &amp;&amp; y &lt; 0 =&gt; &quot;dx-down&quot;,\n\u00a0\u00a0\u00a0\u00a0var (_, _) =&gt; &quot;on border&quot;,\n\u00a0\u00a0\u00a0\u00a0_ =&gt; &quot;unknown&quot;\n};\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Index and intervals<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Two new&nbsp;<em>types&nbsp;<\/em>have been introduced:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>System.Index<\/em>&nbsp;that represents an index in a sequence<\/li><li><em>System.Range<\/em>&nbsp;that represents a secondary interval in a sequence<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">There have been introduced two new operators too:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>the operator \u201c<em>^\u201d<\/em>, which specifies that an index starts from the end of the sequence<\/li><li>the interval operator \u201c<em>..<\/em>\u201d, which specifies the beginning and the end of an interval as operand<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s try to understand, using a&nbsp;<em>daysOfWeek&nbsp;<\/em>matrix:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nvar daysOfWeek = new string&#x5B;]\n{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Index from start\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Index from end\n\u00a0\u00a0\u00a0\u00a0&quot;Monday&quot;,\u00a0\u00a0\u00a0\u00a0 \/\/ 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ^7\n\u00a0\u00a0\u00a0\u00a0&quot;Tuesday&quot;,\u00a0\u00a0\u00a0 \/\/ 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ^6 \n\u00a0\u00a0\u00a0\u00a0&quot;Wednesday&quot;,\u00a0 \/\/ 2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ^5\n\u00a0\u00a0\u00a0\u00a0&quot;Thursday&quot;,\u00a0\u00a0 \/\/ 3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ^4\n\u00a0\u00a0\u00a0\u00a0&quot;Friday&quot;,\u00a0\u00a0\u00a0\u00a0 \/\/ 4\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ^3\n\u00a0\u00a0\u00a0\u00a0&quot;Saturday&quot;,\u00a0\u00a0 \/\/ 5\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ^2\n\u00a0\u00a0\u00a0\u00a0&quot;Sunday&quot;\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 6\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ^1\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ 7 (or daysOfWeek.Length) ^0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \n};\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Index [0] is equal to index [^7] , \u201cMonday\u201d in this case.<br>Index [7] is equal to index [^0], that is daysOfWeek[daysOfWeek.Length], and then they would generate an exception. For any n number, index ^n is equal to daysOfWeek.Length &#8211; n.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An interval (\u201c..\u201d) specifies the beginning and the end of an interval. The beginning of the interval is inclusive, but the end is not included in the interval.<br>We, therefore, have several options to obtain specific intervals:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nvar allDays = daysOfWeek&#x5B;..]; \/\/Contains all the days of week\nvar startOfTheWeek = daysOfWeek&#x5B;..2]; \/\/ &quot;Monday&quot;, &quot;Tuesday&quot;\nvar weekend = daysOfWeek&#x5B;5..]; \/\/ &quot;Saturday&quot;, &quot;Sunday&quot;\nvar someDays = daysOfWeek&#x5B;^5..^3]; \/\/ &quot;Wednesday&quot;, &quot;Thursday&quot;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The two new types,<em>&nbsp;Index&nbsp;<\/em>and&nbsp;<em>Range<\/em>, can be created through the respective constructors, or through a shortcut of C# 8, as in the following example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ daysOfWeek&#x5B;0]\nIndex monday = 0;\n\u00a0\n\/\/ &quot;Wednesday&quot;, &quot;Thursday&quot;\nRange someDays = 2..4;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The interval can then be used as a variable:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nvar days = daysOfWeek&#x5B;someDays];\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Nullable reference types<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As we know, an object can be of a value type, belonging to primitive types such as numbers, struct, etc.<br>They reside in a memory zone called stack and always have a value, even if they are a predefined constant. The reference types, like all the classes and the strings, have to be instantiated in a managed heap and their reference maintained through one variable. This last can assume a null value. To have a null variable means to point at any object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One of the features added in C# 8, that got more prominence, was the introduction of the new&nbsp;<em>Nullable<\/em>&nbsp;flag, which one, if added directly in the file with an extension. csproj, can enable or disable<em>&nbsp;nullable annotation context<\/em>&nbsp;and&nbsp;<em>nullable warning context<\/em>. Valid options are the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>enable&nbsp;<\/strong>&#8211; The&nbsp;<em>nullable annotation contex<\/em>t and the&nbsp;<em>nullable warning context<\/em>&nbsp;are enabled: variables of a reference type are non-nullable. All Null support warnings are enabled.<\/li><li><strong>warnings<\/strong>&nbsp;&#8211; The&nbsp;<em>nullable annotation context&nbsp;<\/em>is disabled, while the&nbsp;<em>nullable warning context&nbsp;<\/em>is enabled: variables of a reference type are independent of values. All<em>&nbsp;null&nbsp;<\/em>support warnings are enabled.<\/li><li><strong>annotations<\/strong>&nbsp;&#8211; The&nbsp;<em>nullable annotation context&nbsp;<\/em>is enabled, while the&nbsp;<em>nullable warning context i<\/em>s disabled: variables of a reference type do not allow&nbsp;<em>null<\/em>&nbsp;values. All&nbsp;<em>null<\/em>&nbsp;support warnings&nbsp;are disabled.<\/li><li><strong>disable<\/strong>&nbsp;&#8211; The<em>&nbsp;nullable annotation context&nbsp;<\/em>and&nbsp;<em>nullable warning context<\/em>&nbsp;are disabled: variables of a reference type are independent of values, as in previous C# versions. All&nbsp;<em>null&nbsp;<\/em>support warnings are disabled.<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&lt;Nullable&gt;enable&lt;\/Nullable&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We are stating that the compiler must check how we use and how we assign any kind of reference.<br>Within a context of&nbsp;<em>nullable annotations<\/em>, any variable of a reference type is considered as a non-nullable reference type. If you want to indicate that a variable can be&nbsp;<em>null<\/em>, you need to add \u201c?\u201d to the name of the type to declare the variable as a nullable reference type.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nstring? name;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">For non-nullable reference types, the compiler uses the flow analysis to ensure that local variables are initialized to a value other than&nbsp;<em>null<\/em>&nbsp;at the time of declaration. Fields must be initialized during construction. A variable must be set by one of the available constructors or by an initializer to a value other than&nbsp;<em>null.<\/em>&nbsp;Moreover, a value that could be&nbsp;<em>null<\/em>&nbsp;cannot be assigned to the reference types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The nullable reference types are not checked to see if they are assigned or initialized to&nbsp;<em>null<\/em>. However, the compiler uses flow analysis to ensure that any variable of a nullable reference type is checked for&nbsp;<em>null&nbsp;<\/em>values before access or before it is assigned to a non-nullable reference type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In essence, if the feature is activated, objects not explicitly declared as&nbsp;<em>Nullable<\/em>&nbsp;will generate a warning in the compilation, if they are set, or there is the possibility that they can assume a null value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can tell the compiler that we are sure that a variable (e.g.:&nbsp;<em>name<\/em>), can\u2019t be null, and then avoid checking it, using the operator\u201c!\u201d.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nname!.Length;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">You can also use directives to set these same contexts anywhere in the project, not only for individual files but also anywhere within a class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>#nullable enable<\/code>:sets nullable annotation context and nullable alarm context on&nbsp;<strong>enabled<\/strong>.<\/li><li><code>#nullable disable<\/code>: sets nullable annotation context and nullable alarm context on&nbsp;<strong>disabled<\/strong>.<\/li><li><code>#nullable restore<\/code>: restores nullable annotation context and nullable alarm context in the project settings.<\/li><li><code>#nullable disable warnings<\/code>: sets the nullable alarm context on&nbsp;<strong>disabled<\/strong>.<\/li><li><code>#nullable enable warnings<\/code>: sets nullable alarm context on&nbsp;<strong>enabled<\/strong>.<\/li><li><code>#nullable restore warnings<\/code>: restores the nullable alarm context in the project settings.<\/li><li><code>#nullable disable annotations<\/code>: :sets nullable annotation context on&nbsp;<strong>disabled<\/strong>.<\/li><li><code>#nullable enable annotations<\/code>: :sets nullable annotation context on&nbsp;<strong>enabled<\/strong>.<\/li><li><code>#nullable restore annotations<\/code>: restores the annotation warning context in the project settings.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">By default, annotation and nullable warning contexts are&nbsp;<strong>disabled<\/strong>. The existing code is then compiled without modification and without generating new warnings.<br>There are many control rules, and I don\u2019t want to go any further. If you are curious about the subject, check the official documentation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Null-coalescing assignment<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The join assignment operator null \u201c?=\u201d was also introduced.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the operator \u201c?=\u201d to assign the value of the right operand of an assignment to the left operand only if the left operand returns null.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nList&lt;int&gt; numbers = null;\nnumbers ??= new List&lt;int&gt;();\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Stackalloc in nested expressions<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<em>stackalloc<\/em>&nbsp;operator allocates a memory block in the&nbsp;<em>stack<\/em>. A memory block allocated to the&nbsp;<em>stack<\/em>, created during the execution of the method, is automatically deleted when the method is returned.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With C# 8, if the result of a&nbsp;<em>stackalloc<\/em>&nbsp;expression is&nbsp;<em>System.Span&lt;T&gt;<\/em>&nbsp;or&nbsp;<em>System.ReadOnlySpan&lt;T&gt;<\/em>, you can use the&nbsp;<em>stackalloc<\/em>&nbsp;expression in other expressions:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nSpan&lt;int&gt; numbers = stackalloc&#x5B;] { 1, 2, 3, 4, 5, 6 };\nvar indx = numbers.IndexOfAny(stackalloc&#x5B;] { 2, 4, 6 ,8 });\nConsole.WriteLine(indx);\u00a0 \/\/ output: 1\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Improvement of interpolated verbatim strings<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Until now, the order of the interpolated string tokens required the token \u201c$\u201d to precede the token \u201c@\u201d.<br>Now, the order of the two tokens is indifferent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The strings:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n$@&quot;...&quot;\n@$&quot;...&quot;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">are both valid interpolated strings.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We conclude our journey to the discovery of the new features of C#8. News are many, and there is much to learn, not least because they are already working on version 9\u2026<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">See you at the next article!<\/p>\n\n\n\n\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s finish the analysis of new features in C# 8<\/p>\n","protected":false},"author":196716244,"featured_media":27386,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"off","_et_pb_old_content":"","_et_gb_content_width":"","_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","inline_featured_image":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_wpcom_ai_launchpad_first_post":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"{title}\n\n{excerpt}\n\n{url}","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false,"jetpack_post_was_ever_published":false},"categories":[688637524],"tags":[688637384],"class_list":["post-28816","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-en","tag-c-en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>New features of C# 8: part two - Blexin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"New features of C# 8: part two - Blexin\" \/>\n<meta property=\"og:description\" content=\"Let\u2019s finish the analysis of new features in C# 8\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/\" \/>\n<meta property=\"og:site_name\" content=\"Blexin\" \/>\n<meta property=\"article:published_time\" content=\"2020-02-19T22:45:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-20T16:40:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i2.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image00-7.png?fit=1024%2C608&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"608\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Francesco Vastarella\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Francesco Vastarella\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/new-features-of-c-8-part-two\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/new-features-of-c-8-part-two\\\/\"},\"author\":{\"name\":\"Francesco Vastarella\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#\\\/schema\\\/person\\\/388dae0ca9df603c88b5e41e29cf2d4d\"},\"headline\":\"New features of C# 8: part two\",\"datePublished\":\"2020-02-19T22:45:00+00:00\",\"dateModified\":\"2021-05-20T16:40:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/new-features-of-c-8-part-two\\\/\"},\"wordCount\":1427,\"image\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/new-features-of-c-8-part-two\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/image00-7.png?fit=1024%2C608&ssl=1\",\"keywords\":[\"C#\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/new-features-of-c-8-part-two\\\/\",\"url\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/new-features-of-c-8-part-two\\\/\",\"name\":\"New features of C# 8: part two - Blexin\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/new-features-of-c-8-part-two\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/new-features-of-c-8-part-two\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/image00-7.png?fit=1024%2C608&ssl=1\",\"datePublished\":\"2020-02-19T22:45:00+00:00\",\"dateModified\":\"2021-05-20T16:40:37+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#\\\/schema\\\/person\\\/388dae0ca9df603c88b5e41e29cf2d4d\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/new-features-of-c-8-part-two\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/new-features-of-c-8-part-two\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/new-features-of-c-8-part-two\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/image00-7.png?fit=1024%2C608&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/image00-7.png?fit=1024%2C608&ssl=1\",\"width\":1024,\"height\":608},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/new-features-of-c-8-part-two\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blexin.com\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"New features of C# 8: part two\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/blexin.com\\\/en\\\/\",\"name\":\"Blexin\",\"description\":\"Con noi \u00e8 semplice\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blexin.com\\\/en\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#\\\/schema\\\/person\\\/388dae0ca9df603c88b5e41e29cf2d4d\",\"name\":\"Francesco Vastarella\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3b8deedae8f35372d5fba49f918006fb0a58a2943aff6ae52d3ff188e0c441bb?s=96&d=identicon&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3b8deedae8f35372d5fba49f918006fb0a58a2943aff6ae52d3ff188e0c441bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3b8deedae8f35372d5fba49f918006fb0a58a2943aff6ae52d3ff188e0c441bb?s=96&d=identicon&r=g\",\"caption\":\"Francesco Vastarella\"},\"url\":\"https:\\\/\\\/blexin.com\\\/en\\\/author\\\/francesco-vastarellablexin-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"New features of C# 8: part two - Blexin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/","og_locale":"en_US","og_type":"article","og_title":"New features of C# 8: part two - Blexin","og_description":"Let\u2019s finish the analysis of new features in C# 8","og_url":"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/","og_site_name":"Blexin","article_published_time":"2020-02-19T22:45:00+00:00","article_modified_time":"2021-05-20T16:40:37+00:00","og_image":[{"width":1024,"height":608,"url":"https:\/\/i2.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image00-7.png?fit=1024%2C608&ssl=1","type":"image\/png"}],"author":"Francesco Vastarella","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Francesco Vastarella","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/#article","isPartOf":{"@id":"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/"},"author":{"name":"Francesco Vastarella","@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/388dae0ca9df603c88b5e41e29cf2d4d"},"headline":"New features of C# 8: part two","datePublished":"2020-02-19T22:45:00+00:00","dateModified":"2021-05-20T16:40:37+00:00","mainEntityOfPage":{"@id":"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/"},"wordCount":1427,"image":{"@id":"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image00-7.png?fit=1024%2C608&ssl=1","keywords":["C#"],"articleSection":["Blog"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/","url":"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/","name":"New features of C# 8: part two - Blexin","isPartOf":{"@id":"https:\/\/blexin.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/#primaryimage"},"image":{"@id":"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image00-7.png?fit=1024%2C608&ssl=1","datePublished":"2020-02-19T22:45:00+00:00","dateModified":"2021-05-20T16:40:37+00:00","author":{"@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/388dae0ca9df603c88b5e41e29cf2d4d"},"breadcrumb":{"@id":"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/#primaryimage","url":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image00-7.png?fit=1024%2C608&ssl=1","contentUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image00-7.png?fit=1024%2C608&ssl=1","width":1024,"height":608},{"@type":"BreadcrumbList","@id":"https:\/\/blexin.com\/en\/blog-en\/new-features-of-c-8-part-two\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blexin.com\/en\/"},{"@type":"ListItem","position":2,"name":"New features of C# 8: part two"}]},{"@type":"WebSite","@id":"https:\/\/blexin.com\/en\/#website","url":"https:\/\/blexin.com\/en\/","name":"Blexin","description":"Con noi \u00e8 semplice","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blexin.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/388dae0ca9df603c88b5e41e29cf2d4d","name":"Francesco Vastarella","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3b8deedae8f35372d5fba49f918006fb0a58a2943aff6ae52d3ff188e0c441bb?s=96&d=identicon&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3b8deedae8f35372d5fba49f918006fb0a58a2943aff6ae52d3ff188e0c441bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3b8deedae8f35372d5fba49f918006fb0a58a2943aff6ae52d3ff188e0c441bb?s=96&d=identicon&r=g","caption":"Francesco Vastarella"},"url":"https:\/\/blexin.com\/en\/author\/francesco-vastarellablexin-com\/"}]}},"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/pcyUBx-7uM","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image00-7.png?fit=1024%2C608&ssl=1","_links":{"self":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/28816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/users\/196716244"}],"replies":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/comments?post=28816"}],"version-history":[{"count":2,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/28816\/revisions"}],"predecessor-version":[{"id":31166,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/28816\/revisions\/31166"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/media\/27386"}],"wp:attachment":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/media?parent=28816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/categories?post=28816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/tags?post=28816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}