还一斤 發表於 2021-12-30 10:13:00

13 个 C# 10 特性

<img style="width: 620px; margin-top: 20px" src="https://blog-1259586045.cos.ap-shanghai.myqcloud.com/sUQ1PGGoW.jpg">
<blockquote>
<p>原文链接:https://blog.okyrylchuk.dev<br>
原文作者:Oleg Kyrylchuk<br>
译: 等天黑</p>
</blockquote>
<h2 id="常量的内插字符串">常量的内插字符串</h2>
<p>C# 10 允许使用在常量字符串初始化中使用插值,如下</p>
<pre><code class="language-csharp">const string name = "Oleg";
const string greeting = $"Hello, {name}.";

Console.WriteLine(greeting);
// Output: Hello, Oleg.

</code></pre>
<h2 id="扩展属性模式">扩展属性模式</h2>
<p>从 C# 10 开始,您可以在适当的模式中引用嵌套的属性或字段, 属性模式变得更具可读性并且需要更少的大括号。</p>
<pre><code class="language-csharp">Person person = new()
{
    Name = "Oleg",
    Location = new() { Country = "PL" }
};

if (person is { Name: "Oleg", Location.Country: "PL" })
{
    Console.WriteLine("It's me!");
}

class Person
{
    public string Name { get; set; }
    public Location Location { get; set; }
}

class Location
{
    public string Country { get; set; }
}

</code></pre>
<p>如果Location为null,则不会匹配模式并返回false。</p>
<h2 id="文件范围的命名空间">文件范围的命名空间</h2>
<p>C# 10 引入了一种新的命名空间声明方式 - 文件范围的命名空间,减少一个大括号,代码结构更简洁。</p>
<pre><code class="language-csharp">namespace FileScopedNamespace;

class Program
{
    static void Main(string[] args)
    {
      Console.WriteLine("Hello World!");
    }
}

</code></pre>
<h2 id="全局-using">全局 Using</h2>
<p>一次引用,全局通用</p>
<pre><code class="language-csharp">global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks;

List&lt;int&gt; list = new() { 1, 2, 3, 4 };
int sum = list.Sum();
Console.WriteLine(sum);

await Task.Delay(1000);

</code></pre>
<h2 id="同一个解构中的赋值和声明">同一个解构中的赋值和声明</h2>
<p>C# 10 可以在同一个解构中进行赋值和声明。</p>
<pre><code class="language-csharp">var rgb = (255, 100, 30);

// Initialization &amp; assignment
int r;
(r, int g, int b) = rgb;

Console.WriteLine($"RGB: {r}, {g}, {b}");
// Output: RGB: 255, 100, 30

</code></pre>
<h2 id="record-类型重写-tostring-时支持密封">Record 类型重写 ToString() 时支持密封</h2>
<pre><code class="language-csharp">Product product = new() { Name = "Bread" };
Console.WriteLine(product.ToString());
// Output: Bread

public record Product
{
    public string Name { get; init; }

    public sealed override string ToString()
    {
      return Name;
    }
}

</code></pre>
<h2 id="record-struct">Record Struct</h2>
<p>C# 10 支持 record struct</p>
<pre><code class="language-csharp">Person me = new() { FirstName = "Oleg", LastName = "Kyrylchuk" };

Console.WriteLine(me);
// Output: Person { FirstName = Oleg, LastName = Kyrylchuk }

Person otherPerson = me with { FirstName = "John" };
Console.WriteLine(otherPerson);
// Output: Person { FirstName = John, LastName = Kyrylchuk }

Person anotherMe = new() { FirstName = "Oleg", LastName = "Kyrylchuk" };
C onsole.WriteLine(me == anotherMe);
// Output: True

record struct Person
{
    public string FirstName { get; init; }
    public string LastName { get; init; }
}

record struct Product(string Name, decimal Price);

</code></pre>
<h2 id="struct-字段支持初始化">Struct 字段支持初始化</h2>
<pre><code class="language-csharp">using System;

Person person = new() { Name = "Oleg" };

Console.WriteLine(person.Id + " " + person.Name);
// Output: 0cc6caac-d061-4f46-9301-c7cc2a012e47 Oleg

struct Person
{
    public Guid Id { get; init; } = Guid.NewGuid();
    public string Name { get; set; }
}

</code></pre>
<h2 id="lambda-表达式的-attributes-支持">Lambda 表达式的 Attributes 支持</h2>
<p>C# 9 支持本地函数的 Attributes, C# 10 添加了 Lambda 表达式的 Attributes 支持。</p>
<pre><code class="language-csharp">Action a = () =&gt; { };               
Action&lt;int&gt; b = (x) =&gt; { };
Action&lt;int&gt; c = ( x) =&gt; { };      


class MyAttribute : Attribute
{ }

</code></pre>
<h2 id="lambda-中的显式返回类型">Lambda 中的显式返回类型</h2>
<pre><code class="language-csharp">Test&lt;int&gt;();

var l1 = string () =&gt; string.Empty;
var l2 = int () =&gt; 0;
var l3 = static void () =&gt; { };

void Test&lt;T&gt;()
{
    var l4 = T () =&gt; default;
}

</code></pre>
<h2 id="应用于方法的-asyncmethodbuilder--特性">应用于方法的 AsyncMethodBuilder特性</h2>
<p>从 C# 7 开始,您只能将<em>AsyncMethodBuilder</em> 特性应用于类型, 在 C# 10 中,您还可以将该特性应用于单个方法。</p>
<pre><code class="language-csharp">using System.Runtime.CompilerServices;

class Example
{
   
    public void ExampleMethod()
    {
    }
}

</code></pre>
<h2 id="结构体中的表达式">结构体中的表达式</h2>
<p>C# 10 支持 将 with 表达式和 struct 一起使用</p>
<pre><code class="language-csharp">Product potato = new() { Name = "Potato", Category = "Vegetable" };
Console.WriteLine($"{potato.Name} {potato.Category}");
// Output: Potato Vegetable

Product tomato = potato with { Name = "Tomato" };
Console.WriteLine($"{tomato.Name} {tomato.Category}");
// Output: Tomato Vegetable

struct Product
{
    public string Name { get; set; }
    public string Category { get; set; }
}

</code></pre>
<h2 id="匿名类型中的表达式">匿名类型中的表达式</h2>
<p>C# 10 支持 将 with 表达式和匿名类型一起使用</p>
<pre><code class="language-csharp">var potato = new { Name = "Potato", Category = "Vegetable" };
Console.WriteLine($"{potato.Name} {potato.Category}");
// Output: Potato Vegetable

var onion = potato with { Name = "Onion" };
Console.WriteLine($"{onion.Name} {onion.Category}");
// Output: Onion Vegetable

</code></pre>
<img style="width: 360px; margin-top: 20px" src="https://blog-1259586045.cos.ap-shanghai.myqcloud.com/wechat_logo_s1.png"><br><br>
来源:https://www.cnblogs.com/myshowtime/p/15747663.html
頁: [1]
查看完整版本: 13 个 C# 10 特性