星雨奇遇 發表於 2023-1-19 19:51:00

C#11新特性整理

<p>假期中有时间,整理了C#11的各个新特性,简单分享给大家。</p>
<p><strong><span style="font-size: 18px">一、使用VSCode新建一个.NET7.0的Console工程</span></strong></p>
<p><img src="https://img2023.cnblogs.com/blog/23525/202301/23525-20230119183214251-1016579769.png" alt="" loading="lazy"></p>
<div class="cnblogs_code">
<pre>&lt;Project Sdk=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Microsoft.NET.Sdk</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
&lt;PropertyGroup&gt;
    &lt;OutputType&gt;Exe&lt;/OutputType&gt;
    &lt;TargetFramework&gt;net7.<span style="color: rgba(128, 0, 128, 1)">0</span>&lt;/TargetFramework&gt;
    &lt;RootNamespace&gt;_NET7&lt;/RootNamespace&gt;
    &lt;ImplicitUsings&gt;enable&lt;/ImplicitUsings&gt;
    &lt;Nullable&gt;enable&lt;/Nullable&gt;
&lt;/PropertyGroup&gt;
&lt;/Project&gt;</pre>
</div>
<p><strong><span style="font-size: 18px">二、泛型属性</span></strong></p>
<p>C # 11为开发人员提供了一种编写自定义通用属性的方法。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> CommonAttribute&lt;T&gt;<span style="color: rgba(0, 0, 0, 1)"> : Attribute
{
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> T Property {<span style="color: rgba(0, 0, 255, 1)">get</span>;<span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">;}

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> CommonAttribute()
    {
      
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> CommonAttribute(T realvalue) :<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">()
    {
      Property </span>=<span style="color: rgba(0, 0, 0, 1)"> realvalue;
    }
}</span></pre>
</div>
<p>新建一个类,在属性上使用这个通用属性注解。原先是多个属性注解类型,现在一个泛型属性就可以搞定了</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> User
{
   
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> UserID { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">;}

   
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> UserName { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">;}
}</span></pre>
</div>
<p><strong><span style="font-size: 18px">三、非空校验</span></strong></p>
<p>原先在C#10中,可以这么写实现参数非空校验</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">User </span>GetUser(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> name)
{
    ArgumentNullException.ThrowIfNull(nameof(name));
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Rest of code</span>
}</pre>
</div>
<p>在C#11中,原计划在参数上加2个!就实现了非空校验,实际测试是不支持的。</p>
<p><strong>error CS8989: 不支持 'parameter null-checking' 功能。</strong></p>
<p><img src="https://img2023.cnblogs.com/blog/23525/202301/23525-20230119192605216-956430330.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p><strong><span style="font-size: 18px">&nbsp;四、字符串内的换行符</span></strong></p>
<p>字符串内插的&nbsp;<code>{</code>&nbsp;和&nbsp;<code>}</code>&nbsp;字符内的文本现在可以跨多个行。&nbsp;</p>
<p><code>{</code>&nbsp;和&nbsp;<code>}</code>&nbsp;标记之间的文本分析为 C#。 允许任何合法 C#(包括换行符)。</p>
<p>使用此功能可以更轻松地读取使用较长 C# 表达式的字符串内插,例如模式匹配&nbsp;<code>switch</code>&nbsp;表达式或 LINQ 查询。</p>
<p><span style="font-size: 18px"><strong>五、列表模式匹配</strong></span></p>
<p>列表模式扩展了模式匹配,以匹配列表或数组中的元素序列。</p>
<p>例如,当&nbsp;<code>sequence</code>&nbsp;为数组或三个整数(1、2 和 3)的列表时,<code>sequence is </code>&nbsp;为&nbsp;<code>true</code>。</p>
<p>可以使用任何模式(包括常量、类型、属性和关系模式)来匹配元素。</p>
<p>弃元模式 (<code>_</code>) 匹配任何单个元素,新的范围模式 (<code>..</code>) 匹配零个或多个元素的任何序列。</p>
<p>写几个列子看看:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> l1 = <span style="color: rgba(0, 0, 255, 1)">new</span>[] { <span style="color: rgba(128, 0, 128, 1)">1</span>, <span style="color: rgba(128, 0, 128, 1)">2</span>, <span style="color: rgba(128, 0, 128, 1)">3</span>, <span style="color: rgba(128, 0, 128, 1)">4</span>, <span style="color: rgba(128, 0, 128, 1)">5</span><span style="color: rgba(0, 0, 0, 1)"> };
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(l1 <span style="color: rgba(0, 0, 255, 1)">is</span> [<span style="color: rgba(128, 0, 128, 1)">1</span>, <span style="color: rgba(128, 0, 128, 1)">2</span>, <span style="color: rgba(128, 0, 128, 1)">3</span>, <span style="color: rgba(128, 0, 128, 1)">4</span>, <span style="color: rgba(128, 0, 128, 1)">5</span>] &amp;&amp; l1 <span style="color: rgba(0, 0, 255, 1)">is</span> )
{
    Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">List Pattern Matched</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
}</span></pre>
</div>
<p><img src="https://img2023.cnblogs.com/blog/23525/202301/23525-20230119194159731-120401825.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p>&nbsp;上述代码中,<span class="token punctuation"> ,可以这么理解:<span class="token comment">匹配长度为5且最后一个元素为5的任何集合。</span></span></span></span></span></span></span></span></p>
<p><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token number"><span class="token punctuation"><span class="token comment"><img src="https://img2023.cnblogs.com/blog/23525/202301/23525-20230119194342177-1980796334.png" alt="" loading="lazy"></span></span></span></span></span></span></span></span></p>
<p>&nbsp;</p>
<p>&nbsp;同时,切片模式进一步放大了列表模式的威力。例如,如果需要将任何少于或等于5个元素的集合与最后一个元素5匹配,则可以将上述模式修改为</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">if</span> (l1 <span style="color: rgba(0, 0, 255, 1)">is</span> [.., <span style="color: rgba(128, 0, 128, 1)">5</span><span style="color: rgba(0, 0, 0, 1)">])
{
    Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">List Pattern Matched 3</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
}</span></pre>
</div>
<p><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token number"><span class="token punctuation"><span class="token comment">&nbsp; &nbsp;这段代码中&nbsp;<code>[..,5]</code>&nbsp;等同于 l1<code>.Length&gt;=1 &amp;&amp; l1[^1]==5</code>.</span></span></span></span></span></span></span></span></p>
<p>&nbsp; 调试输出:List Pattern Matched 3</p>
<p>&nbsp; &nbsp;再写一个Switch模式匹配的例子</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> l1 = <span style="color: rgba(0, 0, 255, 1)">new</span>[] { <span style="color: rgba(128, 0, 128, 1)">1</span>, <span style="color: rgba(128, 0, 128, 1)">2</span>, <span style="color: rgba(128, 0, 128, 1)">3</span>, <span style="color: rgba(128, 0, 128, 1)">4</span>,<span style="color: rgba(128, 0, 128, 1)">5</span><span style="color: rgba(0, 0, 0, 1)"> };
</span><span style="color: rgba(0, 0, 255, 1)">var</span> l2 = <span style="color: rgba(0, 0, 255, 1)">new</span>[] { <span style="color: rgba(128, 0, 128, 1)">1</span>, <span style="color: rgba(128, 0, 128, 1)">3</span>, <span style="color: rgba(128, 0, 128, 1)">5</span><span style="color: rgba(0, 0, 0, 1)"> };
</span><span style="color: rgba(0, 0, 255, 1)">var</span> l3 = <span style="color: rgba(0, 0, 255, 1)">new</span>[] { <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">};
</span><span style="color: rgba(0, 0, 255, 1)">var</span> l4 = <span style="color: rgba(0, 0, 255, 1)">new</span>[] { <span style="color: rgba(128, 0, 128, 1)">9</span><span style="color: rgba(0, 0, 0, 1)"> };
</span><span style="color: rgba(0, 0, 255, 1)">var</span> l5 = <span style="color: rgba(0, 0, 255, 1)">new</span>[] { <span style="color: rgba(128, 0, 128, 1)">1</span>,<span style="color: rgba(128, 0, 128, 1)">6</span><span style="color: rgba(0, 0, 0, 1)"> };

</span><span style="color: rgba(0, 0, 255, 1)">string</span> PatternTest(<span style="color: rgba(0, 0, 255, 1)">int</span>[] collection) =&gt; collection <span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)">
{
    [</span><span style="color: rgba(128, 0, 128, 1)">1</span>, <span style="color: rgba(128, 0, 128, 1)">2</span>, <span style="color: rgba(128, 0, 128, 1)">3</span>, <span style="color: rgba(128, 0, 128, 1)">4</span>, <span style="color: rgba(128, 0, 128, 1)">5</span>] =&gt; <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">first</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> list</span>
    [<span style="color: rgba(128, 0, 128, 1)">1</span>, <span style="color: rgba(128, 0, 128, 1)">3</span>, .., <span style="color: rgba(128, 0, 128, 1)">5</span>] =&gt; <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">second</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> slice in list</span>
    [<span style="color: rgba(128, 0, 128, 1)">1</span>, _] =&gt; <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">third</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> list</span>
    [<span style="color: rgba(128, 0, 128, 1)">1</span>, ..] =&gt; <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">fourth</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">slice in list</span>
    [..] =&gt; <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">fifth</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> slice</span>
<span style="color: rgba(0, 0, 0, 1)">};

Console.WriteLine(PatternTest(l1));
Console.WriteLine(PatternTest(l2));
Console.WriteLine(PatternTest(l3));
Console.WriteLine(PatternTest(l4));
Console.WriteLine(PatternTest(l5));</span></pre>
</div>
<p style="margin-left: 30px"><img src="https://img2023.cnblogs.com/blog/23525/202301/23525-20230119194849049-1710940558.png" alt="" loading="lazy"></p>
<p style="margin-left: 30px"><strong>实际输出:</strong></p>
<p style="margin-left: 30px">first<br>second<br>fourth<br>fifth<br>third</p>
<p><strong><span style="font-size: 18px">&nbsp;六、原始字符串</span></strong></p>
<p style="margin-left: 30px">原始字符串是字符串的一种新格式。</p>
<p style="margin-left: 30px">原始字符串可以包含任意文本,包括空格、新行、嵌入引号和其他特殊字符,<strong>无需转义字符(这个很关键、也很简单)</strong>。</p>
<p style="margin-left: 30px">原始字符串以至少<strong>三个双引号 (""")&nbsp;</strong>字符开头。 它以相同数量的双引号字符结尾。</p>
<p style="margin-left: 30px">通常,原始字符串在单个行上使用三个双引号来开始字符串,在另一行上用三个双引号来结束字符串。 左引号之后、右引号之前的换行符不包括在最终内容中。</p>
<p style="margin-left: 30px">详细的示例,可以看前段时间整理的一篇文章。</p>
<h1 class="postTitle" style="margin-left: 30px"><span style="font-size: 15px">C#11新特性-Raw string literals原始字符串研究、示例</span></h1>
<p>以上是这几天研究的C#11的新特性。分享给大家。</p>
<p>&nbsp;</p>
<p>周国庆</p>
<p>2023/1/19</p>
<div id="gtx-trans" style="position: absolute; left: -5px; top: 1511.48px">&nbsp;</div><br><br>
来源:https://www.cnblogs.com/tianqing/p/17062055.html
頁: [1]
查看完整版本: C#11新特性整理