枫叶巧克力 發表於 2020-3-31 17:02:00

你所不知道的 C# 中的细节

<h2 id="前言">前言</h2>
<p>有一个东西叫做鸭子类型,所谓鸭子类型就是,只要一个东西表现得像鸭子那么就能推出这玩意就是鸭子。</p>
<p>C# 里面其实也暗藏了很多类似鸭子类型的东西,但是很多开发者并不知道,因此也就没法好好利用这些东西,那么今天我细数一下这些藏在编译器中的细节。</p>
<h2 id="不是只有-task-和-valuetask-才能-await">不是只有 <code>Task</code> 和 <code>ValueTask</code> 才能 <code>await</code></h2>
<p>在 C# 中编写异步代码的时候,我们经常会选择将异步代码包含在一个 <code>Task</code> 或者 <code>ValueTask</code> 中,这样调用者就能用 <code>await</code> 的方式实现异步调用。</p>
<p>西卡西,并不是只有 <code>Task</code> 和 <code>ValueTask</code> 才能 <code>await</code>。<code>Task</code> 和 <code>ValueTask</code> 背后明明是由线程池参与调度的,可是为什么 C# 的 <code>async</code>/<code>await</code> 却被说成是 <code>coroutine</code> 呢?</p>
<p>因为你所 <code>await</code> 的东西不一定是 <code>Task</code>/<code>ValueTask</code>,在 C# 中只要你的类中包含 <code>GetAwaiter()</code> 方法和 <code>bool IsCompleted</code> 属性,并且 <code>GetAwaiter()</code> 返回的东西包含一个 <code>GetResult()</code> 方法、一个 <code>bool IsCompleted</code> 属性和实现了 <code>INotifyCompletion</code>,那么这个类的对象就是可以 <code>await</code> 的 。</p>
<p>因此在封装 I/O 操作的时候,我们可以自行实现一个 <code>Awaiter</code>,它基于底层的 <code>epoll</code>/<code>IOCP</code> 实现,这样当 <code>await</code> 的时候就不会创建出任何的线程,也不会出现任何的线程调度,而是直接让出控制权。而 OS 在完成 I/O 调用后通过 <code>CompletionPort</code> (Windows) 等通知用户态完成异步调用,此时恢复上下文继续执行剩余逻辑,这其实就是一个真正的 <code>stackless coroutine</code>。</p>
<pre><code class="language-csharp">public class MyTask&lt;T&gt;
{
    public MyAwaiter&lt;T&gt; GetAwaiter()
    {
      return new MyAwaiter&lt;T&gt;();
    }
}

public class MyAwaiter&lt;T&gt; : INotifyCompletion
{
    public bool IsCompleted { get; private set; }
    public T GetResult()
    {
      throw new NotImplementedException();
    }
    public void OnCompleted(Action continuation)
    {
      throw new NotImplementedException();
    }
}

public class Program
{
    static async Task Main(string[] args)
    {
      var obj = new MyTask&lt;int&gt;();
      await obj;
    }
}
</code></pre>
<p>事实上,.NET Core 中的 I/O 相关的异步 API 也的确是这么做的,I/O 操作过程中是不会有任何线程分配等待结果的,都是 <code>coroutine</code> 操作:I/O 操作开始后直接让出控制权,直到 I/O 操作完毕。而之所以有的时候你发现 <code>await</code> 前后线程变了,那只是因为 <code>Task</code> 本身被调度了。</p>
<p>UWP 开发中所用的 <code>IAsyncAction</code>/<code>IAsyncOperation&lt;T&gt;</code> 则是来自底层的封装,和 <code>Task</code> 没有任何关系但是是可以 <code>await</code> 的,并且如果用 C++/WinRT 开发 UWP 的话,返回这些接口的方法也都是可以 <code>co_await</code> 的。</p>
<h2 id="不是只有-ienumerable-和-ienumerator-才能被-foreach">不是只有 <code>IEnumerable</code> 和 <code>IEnumerator</code> 才能被 <code>foreach</code></h2>
<p>经常我们会写如下的代码:</p>
<pre><code class="language-csharp">foreach (var i in list)
{
    // ......
}
</code></pre>
<p>然后一问为什么可以 <code>foreach</code>,大多都会回复因为这个 <code>list</code> 实现了 <code>IEnumerable</code> 或者 <code>IEnumerator</code>。</p>
<p>但是实际上,如果想要一个对象可被 <code>foreach</code>,只需要提供一个 <code>GetEnumerator()</code> 方法,并且 <code>GetEnumerator()</code> 返回的对象包含一个 <code>bool MoveNext()</code> 方法加一个 <code>Current</code> 属性即可。</p>
<pre><code class="language-csharp">class MyEnumerator&lt;T&gt;
{
    public T Current { get; private set; }
    public bool MoveNext()
    {
      throw new NotImplementedException();
    }
}
   
class MyEnumerable&lt;T&gt;
{
    public MyEnumerator&lt;T&gt; GetEnumerator()
    {
      throw new NotImplementedException();
    }
}

class Program
{
    public static void Main()
    {
      var x = new MyEnumerable&lt;int&gt;();
      foreach (var i in x)
      {
            // ......
      }
    }
}
</code></pre>
<h2 id="不是只有-iasyncenumerable-和-iasyncenumerator-才能被-await-foreach">不是只有 <code>IAsyncEnumerable</code> 和 <code>IAsyncEnumerator</code> 才能被 <code>await foreach</code></h2>
<p>同上,但是这一次要求变了,<code>GetEnumerator()</code> 和 <code>MoveNext()</code> 变为 <code>GetAsyncEnumerator()</code> 和 <code>MoveNextAsync()</code>。</p>
<p>其中 <code>MoveNextAsync()</code> 返回的东西应该是一个 <code>Awaitable&lt;bool&gt;</code>,至于这个 <code>Awaitable</code> 到底是什么,它可以是 <code>Task</code>/<code>ValueTask</code>,也可以是其他的或者你自己实现的。</p>
<pre><code class="language-csharp">class MyAsyncEnumerator&lt;T&gt;
{
    public T Current { get; private set; }
    public MyTask&lt;bool&gt; MoveNextAsync()
    {
      throw new NotImplementedException();
    }
}
   
class MyAsyncEnumerable&lt;T&gt;
{
    public MyAsyncEnumerator&lt;T&gt; GetAsyncEnumerator()
    {
      throw new NotImplementedException();
    }
}

class Program
{
    public static async Task Main()
    {
      var x = new MyAsyncEnumerable&lt;int&gt;();
      await foreach (var i in x)
      {
            // ......
      }
    }
}
</code></pre>
<h2 id="ref-struct-要怎么实现-idisposable"><code>ref struct</code> 要怎么实现 <code>IDisposable</code></h2>
<p>众所周知 <code>ref struct</code> 因为必须在栈上且不能被装箱,所以不能实现接口,但是如果你的 <code>ref struct</code> 中有一个 <code>void Dispose()</code> 那么就可以用 <code>using</code> 语法实现对象的自动销毁。</p>
<pre><code class="language-csharp">ref struct MyDisposable
{
    public void Dispose() =&gt; throw new NotImplementedException();
}

class Program
{
    public static void Main()
    {
      using var y = new MyDisposable();
      // ......
    }
}
</code></pre>
<h2 id="不是只有-range-才能使用切片">不是只有 <code>Range</code> 才能使用切片</h2>
<p>C# 8 引入了 Ranges,允许切片操作,但是其实并不是必须提供一个接收 <code>Range</code> 类型参数的 indexer 才能使用该特性。</p>
<p>只要你的类可以被计数(拥有 <code>Length</code> 或 <code>Count</code> 属性),并且可以被切片(拥有一个 <code>Slice(int, int)</code> 方法),那么就可以用该特性。</p>
<pre><code class="language-csharp">class MyRange
{
    public int Count { get; private set; }
    public object Slice(int x, int y) =&gt; throw new NotImplementedException();
}

class Program
{
    public static void Main()
    {
      var x = new MyRange();
      var y = x;
    }
}
</code></pre>
<h2 id="不是只有-index-才能使用索引">不是只有 <code>Index</code> 才能使用索引</h2>
<p>C# 8 引入了 Indexes 用于索引,例如使用 <code>^1</code> 索引倒数第一个元素,但是其实并不是必须提供一个接收 <code>Index</code> 类型参数的 indexer 才能使用该特性。</p>
<p>只要你的类可以被计数(拥有 <code>Length</code> 或 <code>Count</code> 属性),并且可以被索引(拥有一个接收 <code>int</code> 参数的索引器),那么就可以用该特性。</p>
<pre><code class="language-csharp">class MyIndex
{
    public int Count { get; private set; }
    public object this
    {
      get =&gt; throw new NotImplementedException();
    }
}

class Program
{
    public static void Main()
    {
      var x = new MyIndex();
      var y = x[^1];
    }
}
</code></pre>
<h2 id="给类型实现解构">给类型实现解构</h2>
<p>如何给一个类型实现解构呢?其实只需要写一个名字为 <code>Deconstruct()</code> 的方法,并且参数都是 <code>out</code> 的即可。</p>
<pre><code class="language-csharp">class MyDeconstruct
{
    private int A =&gt; 1;
    private int B =&gt; 2;
    public void Deconstruct(out int a, out int b)
    {
      a = A;
      b = B;
    }
}

class Program
{
    public static void Main()
    {
      var x = new MyDeconstruct();
      var (o, u) = x;
    }
}
</code></pre>
<h2 id="不是只有实现了-ienumerable-才能用-linq">不是只有实现了 <code>IEnumerable</code> 才能用 <code>LINQ</code></h2>
<p><code>LINQ</code> 是 C# 中常用的一种集成查询语言,允许你这样写代码:</p>
<pre><code class="language-csharp">from c in list where c.Id &gt; 5 select c;
</code></pre>
<p>但是上述代码中的 <code>list</code> 的类型不一定非得实现 <code>IEnumerable</code>,事实上,只要有对应名字的扩展方法就可以了,比如有了叫做 <code>Select</code> 的方法就能用 <code>select</code>,有了叫做 <code>Where</code> 的方法就能用 <code>where</code>。</p>
<pre><code class="language-csharp">class Just&lt;T&gt; : Maybe&lt;T&gt;
{
    private readonly T value;
    public Just(T value) { this.value = value; }

    public override Maybe&lt;U&gt; Select&lt;U&gt;(Func&lt;T, Maybe&lt;U&gt;&gt; f) =&gt; f(value);
    public override string ToString() =&gt; $"Just {value}";
}

class Nothing&lt;T&gt; : Maybe&lt;T&gt;
{
    public override Maybe&lt;U&gt; Select&lt;U&gt;(Func&lt;T, Maybe&lt;U&gt;&gt; _) =&gt; new Nothing&lt;U&gt;();
    public override string ToString() =&gt; "Nothing";
}

abstract class Maybe&lt;T&gt;
{
    public abstract Maybe&lt;U&gt; Select&lt;U&gt;(Func&lt;T, Maybe&lt;U&gt;&gt; f);

    public Maybe&lt;V&gt; SelectMany&lt;U, V&gt;(Func&lt;T, Maybe&lt;U&gt;&gt; k, Func&lt;T, U, V&gt; s)
      =&gt; Select(x =&gt; k(x).Select(y =&gt; new Just&lt;V&gt;(s(x, y))));

    public Maybe&lt;U&gt; Where(Func&lt;Maybe&lt;T&gt;, bool&gt; f) =&gt; f(this) ? this : new Nothing&lt;T&gt;();
}

class Program
{
    public static void Main()
    {
      var x = new Just&lt;int&gt;(3);
      var y = new Just&lt;int&gt;(7);
      var z = new Nothing&lt;int&gt;();

      var u = from x0 in x from y0 in y select x0 + y0;
      var v = from x0 in x from z0 in z select x0 + z0;
      var just = from c in x where true select c;
      var nothing = from c in x where false select c;
    }
}
</code></pre><br><br>
来源:https://www.cnblogs.com/hez2010/p/12606419.html
頁: [1]
查看完整版本: 你所不知道的 C# 中的细节