<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="/rss2.0-to-html.xslt" media="screen" version="1.0"?>

<rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
  xmlns:admin="http://webns.net/mvcb/"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:webfeeds="http://webfeeds.org/rss/1.0">

  <channel>
    <title>Curiosity is bliss</title>
    <link>https://blog.monstuff.com</link>
    <description>Julien Couvreur's programming blog and more</description>
    <language>en-us</language>
    <pubDate>2026-04-11T13:08:46-07:00</pubDate>
    <managingEditor>Julien Couvreur</managingEditor>
    <webfeeds:analytics id="UA-74363-1" engine="GoogleAnalytics"/>

    <!--
        <webfeeds:cover image="http://yoursite.com/a-large-cover-image.png" />
        <webfeeds:icon>http://feedly.com/icon.svg</webfeeds:icon>
-->


    <item>
      <title>Analyzing a nullability example</title>
      <link>/archives/2020/01/analyzing-a-nullability-example.html</link>
      <description><![CDATA[<p>Cezary Piątek posted a good <a href="https://cezarypiatek.github.io/post/non-nullable-references-in-dotnet-core/">overview of the C# nullable reference types feature</a>. 
It includes a <a href="https://cezarypiatek.github.io/post/non-nullable-references-in-dotnet-core/#there-is-still-room-for-improvement">critique of a code snippet</a>. Examining that snippet is a good way to understand some of the C# LDM’s decisions.
In the following, Cezary expects a warning on a seemingly unreachable branch and no warning on the dereference.</p>

<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="err">#</span><span class="n">nullable</span> <span class="n">enable</span>

<span class="k">public</span> <span class="k">class</span> <span class="nc">User</span> 
<span class="p">{</span>
    <span class="k">public</span> <span class="k">static</span> <span class="k">void</span> <span class="nf">Method</span><span class="p">(</span><span class="n">User</span> <span class="n">userEntity</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">userEntity</span> <span class="p">==</span> <span class="k">null</span><span class="p">)</span> <span class="c1">// Actual: no warning for seemingly unreachable branch. </span>
        <span class="p">{</span>
        <span class="p">}</span>
        
        <span class="kt">var</span> <span class="n">s</span> <span class="p">=</span> <span class="n">userEntity</span><span class="p">.</span><span class="nf">ToString</span><span class="p">();</span> <span class="c1">// Actual: warning CS8602: Dereference of a possibly null reference.</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>
<p><a href="https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA+BiAdgVwDZ4CGweMABDFsaQLABQ9AAgMxmMBMZAqgM4xRl6Ab3pkxbVowCMSNijIBZGABcAFhAAmACl78yOPlACiWZQEtlATwCUo8SLrinZMwDMyWg/xPmrZALz+ZLgE1mQA9OHBEGQA7oRQWGZYAOZkrtBkfDAAtskpeJb6WLCEYKrU5MBQhFjlgo7OYg5NYgC+dk2dzgBuCVkB+oY+FpYAdAAqEADKylD5WtYA3BFR8Yn5ZADC0wAcSAAM7CBkACL8MK4XdeQQ7oRkAA4QPDxmJEUheGSwV7A3Y26HTobSAA=">sharplab</a></p>

<p>Why is there no warning on the seemingly unnecessary null test <code class="language-plaintext highlighter-rouge">if (userEntity == null) ...</code> or the apparently unreachable branch?</p>

<p>It’s because such tests are useful and encouraged in public APIs. Users should check inputs and the compiler should not get in the way of good practices. The branch of the <code class="language-plaintext highlighter-rouge">if</code> is therefore reachable.</p>

<p>Then, what is the state of <code class="language-plaintext highlighter-rouge">userEntity</code> within the <code class="language-plaintext highlighter-rouge">if</code> block?</p>

<p>We take the user’s null test seriously by considering <code class="language-plaintext highlighter-rouge">userEntity</code> to be maybe-null within the <code class="language-plaintext highlighter-rouge">if</code> block. So if the user did <code class="language-plaintext highlighter-rouge">userEntity.ToString()</code> inside the <code class="language-plaintext highlighter-rouge">if</code>, the compiler would rightly warn. This protects the user against a null reference exception that could realistically happen.</p>

<p>Given those, what should be the state of the <code class="language-plaintext highlighter-rouge">userEntity</code> at the exit of the <code class="language-plaintext highlighter-rouge">if</code>?</p>

<p>Because we’re merging branches where <code class="language-plaintext highlighter-rouge">userEntity</code> is maybe-null (when the condition of the <code class="language-plaintext highlighter-rouge">if</code> is true) and not-null (in the alternative), the state of <code class="language-plaintext highlighter-rouge">userEntity</code> is maybe-null. Therefore we warn on dereference on <code class="language-plaintext highlighter-rouge">userEntity</code> after the <code class="language-plaintext highlighter-rouge">if</code>.
Note that if the <code class="language-plaintext highlighter-rouge">if</code> block contained a <code class="language-plaintext highlighter-rouge">throw</code>, the <code class="language-plaintext highlighter-rouge">userEntity</code> would be considered not-null after the <code class="language-plaintext highlighter-rouge">if</code>. This is a common patter: <code class="language-plaintext highlighter-rouge">if (userEntity is null) throw new ArgumentNullException(nameof(userEntity));</code>.</p>
]]></description>
      <guid isPermaLink="true">https://blog.monstuff.com/archives/2020/01/analyzing-a-nullability-example.html</guid>
      <pubDate>2020-01-15T00:00:00-08:00</pubDate>
    </item>
    <item>
      <title>My 2019 EconTalk favorites</title>
      <link>/archives/2020/01/my-2019-econtalk-favorites.html</link>
      <description><![CDATA[<ul>
  <li><a href="https://www.econtalk.org/susan-mayer-on-what-money-cant-buy/">Susan Mayer on What Money Can’t Buy</a> (on effectively helping kids in poverty)</li>
  <li><a href="https://www.econtalk.org/mauricio-miller-on-poverty-social-work-and-the-alternative/">Mauricio Miller on Poverty, Social Work, and the Alternative</a> (on effectively helping families in poverty)</li>
  <li><a href="https://www.econtalk.org/keith-smith-on-free-market-health-care/">Keith Smith on Free Market Health Care</a> (on unintended effects of healthcare policies)</li>
  <li><a href="https://www.econtalk.org/rory-sutherland-on-alchemy/">Rory Sutherland on Alchemy</a> (on subjective value, the benefits of marketing and lateral thinking)</li>
  <li><a href="https://www.econtalk.org/anja-shortland-on-kidnap/">Anja Shortland on Kidnap</a> (on the economics and game theory of kidnapping)</li>
  <li><a href="https://www.econtalk.org/bjorn-lomborg-on-the-costs-and-benefits-of-attacking-climate-change/">Bjorn Lomborg on the Costs and Benefits of Attacking Climate Change</a> (on the trade-offs of climate policies)</li>
</ul>
]]></description>
      <guid isPermaLink="true">https://blog.monstuff.com/archives/2020/01/my-2019-econtalk-favorites.html</guid>
      <pubDate>2020-01-13T00:00:00-08:00</pubDate>
    </item>
    <item>
      <title>Overview of nullability analysis</title>
      <link>/archives/2019/05/nullability-analysis.html</link>
      <description><![CDATA[<p>A regular Roslyn contributor, <a href="https://github.com/YairHalberstadt">Yair</a>, asked for some pointers about C# 8.0’s nullability analysis on the <a href="https://gitter.im/dotnet/roslyn">gitter</a> channel. I thought I’d expand on my reply and share it more broadly.</p>

<p>This post assumes familiarity with the “nullable reference types” feature, including the concepts of nullability <a href="https://github.com/dotnet/csharplang/blob/master/proposals/csharp-8.0/nullable-reference-types-specification.md#nullability-of-types">annotations</a> (annotated, not-annotated, oblivious) and <a href="https://github.com/dotnet/csharplang/blob/master/proposals/csharp-8.0/nullable-reference-types-specification.md#null-state-and-null-tracking">states</a> (not-null, maybe-null).</p>

<h2 id="bound-trees">Bound trees</h2>

<p>The backbone of the compiler consists of four main stages:</p>
<ul>
  <li><em>parsing</em> source code into syntax trees,</li>
  <li>building symbols from declarations and <em>binding</em> the syntax of each method body into an initial bound tree,</li>
  <li><em>lowering</em> the bound tree into a set of simpler bound nodes,</li>
  <li><em>emitting</em> IL from the lowered bound nodes, along with some metadata.</li>
</ul>

<p>Nullability analysis rests on the initial bound tree. This tree has a structure similar to the syntax tree, but instead of referencing un-interpreted identifiers (like <code class="language-plaintext highlighter-rouge">x</code> or <code class="language-plaintext highlighter-rouge">Method</code>) it references symbols. Symbols are an object model for entities declared by a program or loaded from metadata. 
For example, symbols allow differentiating different uses of a given identifier in code. You could have a parameter <code class="language-plaintext highlighter-rouge">x</code>, a local <code class="language-plaintext highlighter-rouge">x</code>, a type <code class="language-plaintext highlighter-rouge">x</code> or even a method <code class="language-plaintext highlighter-rouge">x</code>. For each kind of symbol you can ask different questions, such as the type of the parameter or local, or the return and parameter types of a method.</p>

<p>When types are explicit in source (for example, <code class="language-plaintext highlighter-rouge">string nonNullLocal = "";</code>, <code class="language-plaintext highlighter-rouge">string? maybeNullLocal = "";</code> or <code class="language-plaintext highlighter-rouge">MakeArray&lt;string?&gt;(item)</code>), the bound nodes and symbols capture an explicit/declared nullability: <code class="language-plaintext highlighter-rouge">TypeWithAnnotations</code> with <code class="language-plaintext highlighter-rouge">Annotated</code> or <code class="language-plaintext highlighter-rouge">NotAnnotated</code> annotations in a <a href="https://github.com/dotnet/csharplang/blob/master/proposals/csharp-8.0/nullable-reference-types-specification.md#nullable-contexts">context</a> with nullability annotations enabled, or <code class="language-plaintext highlighter-rouge">Oblivious</code> in a disabled context.</p>

<p>When types are inferred (for example, in <code class="language-plaintext highlighter-rouge">var local = "";</code> or <code class="language-plaintext highlighter-rouge">MakeArray(item)</code>), the bound node just uses an <code class="language-plaintext highlighter-rouge">Oblivious</code> annotation, which the nullability analysis will later revise.</p>

<h2 id="nullablewalker">NullableWalker</h2>

<p><a href="https://github.com/dotnet/roslyn/blob/master/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs"><code class="language-plaintext highlighter-rouge">NullableWalker</code></a> is responsible for most of the analysis. It is a visitor for the initial bound tree, which:</p>
<ol>
  <li>computes the nullability state of expressions (and save those to answer queries from the IDE),</li>
  <li>keeps track of knowledge for different variables (more on that below), and</li>
  <li>produces warnings.</li>
</ol>

<h2 id="state-tracking">State tracking</h2>

<p>As the analysis progresses through a method body, <code class="language-plaintext highlighter-rouge">NullableWalker</code> tracks some knowledge for each variable (or more generally, storage location). At a certain point in the analysis, the state for a given variable is either <code class="language-plaintext highlighter-rouge">MaybeNull</code> or <code class="language-plaintext highlighter-rouge">NotNull</code>.
For all the tracked variables, this is represented as a state array, in which each variable gets an index/position/slot.</p>

<p>For instance, each parameter and local in a method gets a slot, which holds either a <code class="language-plaintext highlighter-rouge">NotNull</code> or <code class="language-plaintext highlighter-rouge">MaybeNull</code> state. Consider a parameter <code class="language-plaintext highlighter-rouge">string? p1</code>: we give it a slot/index and we’ll initialize its state to maybe-null (ie. <code class="language-plaintext highlighter-rouge">State[slot] = MaybeNull</code>, because its declared type is <code class="language-plaintext highlighter-rouge">Annotated</code>), then when we visit <code class="language-plaintext highlighter-rouge">p1 = "";</code> we can just override that state, and when we visit <code class="language-plaintext highlighter-rouge">p1.ToString()</code> we consult that state to decide whether to warn for possible null dereference.</p>

<p><code class="language-plaintext highlighter-rouge">NullableWalker</code> not only tracks variables, it also tracks fields, so it assigns slots for those too. That way, it can warn on <code class="language-plaintext highlighter-rouge">localStruct.field1.ToString()</code>, but not <code class="language-plaintext highlighter-rouge">localStruct.field2.ToString()</code> independently.
Such nested slots are known to have a containing slot. With that information, we can look at an assignment like <code class="language-plaintext highlighter-rouge">local2 = local1;</code> and we can not only copy the slot for <code class="language-plaintext highlighter-rouge">local1</code> to set the state of <code class="language-plaintext highlighter-rouge">local2</code>, but we can copy the nested slots thereby transfering all of our knowledge of <code class="language-plaintext highlighter-rouge">local1</code> to <code class="language-plaintext highlighter-rouge">local2</code>.</p>

<p>The state is generally just a simple array, but it can also be two arrays in some cases. That’s called “conditional state”. It is used for analyzing expressions like <code class="language-plaintext highlighter-rouge">x == null</code>. We keep track of the states “if the expression were true” and “if the expression were false” separately. Slots are still used to index into those arrays as normal.</p>

<p>Cloning states is another common operation. When analyzing <code class="language-plaintext highlighter-rouge">if (b) ... else ...</code>, we clone the state so that we can analyze each branch separately. We can merge those states when the branches rejoin (<code class="language-plaintext highlighter-rouge">Meet</code> takes the worst case values). That gives us the state following the <code class="language-plaintext highlighter-rouge">if</code> statement.</p>

<p>In code that isn’t reachable, as in <code class="language-plaintext highlighter-rouge">if (false) { ... unreachable ...}</code>, every value you read is <code class="language-plaintext highlighter-rouge">NotNull</code> regardless of tracked state to minimize warnings.</p>

<h2 id="simple-example">Simple example</h2>

<p>Let’s wrap up this overview by looking at an assignment, <code class="language-plaintext highlighter-rouge">x = y</code>. To analyze this expression, we’re going to:</p>
<ol>
  <li>visit the right-hand-side expression and get a <code class="language-plaintext highlighter-rouge">TypeWithState</code> back which tells us the null-state of <code class="language-plaintext highlighter-rouge">y</code> at this point in the program,</li>
  <li>visit the left-hand-side expression as an L-value (i.e., for assigning to) and get a <code class="language-plaintext highlighter-rouge">TypeWithAnnotations</code> back which tells us the declared type of <code class="language-plaintext highlighter-rouge">x</code> (not its state),</li>
  <li>we check if the assignment from the state of <code class="language-plaintext highlighter-rouge">y</code> to the declared type of <code class="language-plaintext highlighter-rouge">x</code> poses problems, both in terms of top-level nullability (for instance, are we assigning a <code class="language-plaintext highlighter-rouge">null</code> value to a un-annotated <code class="language-plaintext highlighter-rouge">string</code> variable?), or nested nullability (for example, are we assigning a <code class="language-plaintext highlighter-rouge">List&lt;string&gt;</code> value to a <code class="language-plaintext highlighter-rouge">List&lt;string?&gt;</code> variable?),</li>
  <li>we update the state of <code class="language-plaintext highlighter-rouge">x</code> based on the state of <code class="language-plaintext highlighter-rouge">y</code>,</li>
  <li>return the state of <code class="language-plaintext highlighter-rouge">x</code> as the state of the assignment expression, in case it is a nested expression like <code class="language-plaintext highlighter-rouge">(x = y).ToString()</code>.</li>
</ol>

<p>In that example, <code class="language-plaintext highlighter-rouge">y</code> might not be a simple bound node for accessing <code class="language-plaintext highlighter-rouge">y</code>, but it could also involve implicit conversions. In that case, visiting <code class="language-plaintext highlighter-rouge">y</code> at the step (1) will visit a bound conversion which holds <code class="language-plaintext highlighter-rouge">y</code> as its operand. As long as the visit operation for each kind of bound node does its part (i.e., produce a <code class="language-plaintext highlighter-rouge">TypeWithState</code> for the expression, produce proper side effects on state and diagnostics) then this process composes well.</p>
]]></description>
      <guid isPermaLink="true">https://blog.monstuff.com/archives/2019/05/nullability-analysis.html</guid>
      <pubDate>2019-05-14T00:00:00-07:00</pubDate>
    </item>
    <item>
      <title>Async Enumerables with Cancellation</title>
      <link>/archives/2019/03/async-enumerables-with-cancellation.html</link>
      <description><![CDATA[<p>In this post, I’ll explain how to produce and consume async enumerables with support for cancellation.</p>

<p>Let’s start with some context. C# 8.0 added support for async-streams, which is composed of three parts:</p>
<ol>
  <li>async-iterator methods: you can write methods with the <code class="language-plaintext highlighter-rouge">async</code> modifier, returning either <code class="language-plaintext highlighter-rouge">IAsyncEnumerable</code> or <code class="language-plaintext highlighter-rouge">IAsyncEnumerator</code>, and  using both <code class="language-plaintext highlighter-rouge">yield</code> and <code class="language-plaintext highlighter-rouge">await</code> syntax.</li>
  <li><code class="language-plaintext highlighter-rouge">await foreach</code>: you can asynchronously enumerate collections that implement <code class="language-plaintext highlighter-rouge">IAsyncEnumerable</code> (or implement equivalent APIs).</li>
  <li><code class="language-plaintext highlighter-rouge">await using</code>: you can asynchronously dispose resources that implement <code class="language-plaintext highlighter-rouge">IAsyncDisposable</code>.</li>
</ol>

<h3 id="await-foreach"><code class="language-plaintext highlighter-rouge">await foreach</code></h3>

<p><code class="language-plaintext highlighter-rouge">await foreach</code> follows a similar execution pattern as its synchronous sibling <code class="language-plaintext highlighter-rouge">foreach</code>: it first gets an enumerator from the enumerable (by calling <code class="language-plaintext highlighter-rouge">GetAsyncEnumerator()</code>, then repeatedly does <code class="language-plaintext highlighter-rouge">await MoveNextAsync()</code> on the enumerator and gets the item with <code class="language-plaintext highlighter-rouge">Current</code> until the enumerator is exhausted.</p>

<p>Here’s the code generated for an <code class="language-plaintext highlighter-rouge">await foreach</code>:</p>

<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">E</span> <span class="n">e</span> <span class="p">=</span> <span class="p">((</span><span class="n">C</span><span class="p">)(</span><span class="n">x</span><span class="p">)).</span><span class="nf">GetAsyncEnumerator</span><span class="p">();</span>
<span class="k">try</span>
<span class="p">{</span>
    <span class="k">while</span> <span class="p">(</span><span class="k">await</span> <span class="n">e</span><span class="p">.</span><span class="nf">MoveNextAsync</span><span class="p">())</span>
    <span class="p">{</span>
        <span class="n">V</span> <span class="n">v</span> <span class="p">=</span> <span class="p">(</span><span class="n">V</span><span class="p">)(</span><span class="n">T</span><span class="p">)</span><span class="n">e</span><span class="p">.</span><span class="n">Current</span><span class="p">;</span>
        <span class="c1">// body</span>
    <span class="p">}</span>
<span class="p">}</span>
<span class="k">finally</span>
<span class="p">{</span>
    <span class="k">await</span> <span class="n">e</span><span class="p">.</span><span class="nf">DisposeAsync</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div></div>

<p>You may notice in the relevant APIs (copied below) that <code class="language-plaintext highlighter-rouge">GetAsyncEnumerator</code> accepts a <code class="language-plaintext highlighter-rouge">CancellationToken</code> parameter. But <code class="language-plaintext highlighter-rouge">await foreach</code> doesn’t make use of this parameter (it passes a <code class="language-plaintext highlighter-rouge">default</code> value).</p>

<p>This raises two questions: 1) how do you write an async enumerable with support for cancellation? and 2) how do you consume one?</p>

<h3 id="writing-an-async-enumerable-supporting-cancellation">Writing an async enumerable supporting cancellation</h3>

<p>Let’s say that you intend to write <code class="language-plaintext highlighter-rouge">IAsyncEnumerable&lt;int&gt; GetItemsAsync(int maxItems)</code> supporting cancellation.</p>

<p>If you just declared the method as <code class="language-plaintext highlighter-rouge">async IAsyncEnumerable&lt;int&gt; GetItemsAsync(int maxItems, CancellationToken token)</code>, you would be able to pass a cancellation token as an argument, then use it in the body of the method.<br />
But the resulting async-iterator would not properly implement the <code class="language-plaintext highlighter-rouge">IAsyncEnumerable.GetAsyncEnumerator(CancellationToken)</code> API, because it would drop the cancellation token passed to it.<br />
The solution is to declare the method as <code class="language-plaintext highlighter-rouge">async IAsyncEnumerable&lt;int&gt; GetItemsAsync(int maxItems, [EnumeratorCancellation] CancellationToken token)</code>.
Because of the attribute, the <code class="language-plaintext highlighter-rouge">token</code> parameter will be set to a synthesized cancellation token that combines two token: the one passed as an argument to the method, and the other given to <code class="language-plaintext highlighter-rouge">GetAsyncEnumerator</code>. This synthesized token gets cancelled when either of the two given tokens is cancelled.</p>

<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">async</span> <span class="n">IAsyncEnumerable</span><span class="p">&lt;</span><span class="kt">int</span><span class="p">&gt;</span> <span class="nf">GetItemsAsync</span><span class="p">(</span><span class="kt">int</span> <span class="n">maxItems</span><span class="p">,</span> <span class="p">[</span><span class="n">EnumeratorCancellation</span><span class="p">]</span> <span class="n">CancellationToken</span> <span class="n">token</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">// Your method body using:</span>
    <span class="c1">// - `_maxItems`</span>
    <span class="c1">// - `token.ThrowIfCancelled();`</span>
    <span class="c1">// - `await` and `yield` constructs</span>
<span class="p">}</span>
</code></pre></div></div>

<h3 id="consuming-an-async-enumerable-with-cancellation">Consuming an async enumerable with cancellation</h3>

<p>There are two scenarios for consuming an async enumerable:</p>
<ol>
  <li>If the method that creates the async enumerable has a cancellation token parameter marked with <code class="language-plaintext highlighter-rouge">[EnumeratorCancellation]</code>, then just call that method with the cancellation token you need:<br />
 <code class="language-plaintext highlighter-rouge">await foreach (var item in GetItemsAsync(maxItems: 10, token)) ...</code></li>
  <li>If the async enumerable instance is given to you, or is constructed in a way to doesn’t capture the desired cancellation token, then you can feed the cancellation token using the  <code class="language-plaintext highlighter-rouge">WithCancellation&lt;T&gt;(this IAsyncEnumerable&lt;T&gt; source, CancellationToken cancellationToken)</code> <a href="https://github.com/dotnet/coreclr/pull/21939">extension method</a>:<br />
 <code class="language-plaintext highlighter-rouge">await foreach (var item in asyncEnumerable.WithCancellation(token)) ...</code></li>
</ol>

<p>The <code class="language-plaintext highlighter-rouge">WithCancellation</code> helper method wraps the enumerable from <code class="language-plaintext highlighter-rouge">GetItemsAsync</code> along with the given cancellation token. When <code class="language-plaintext highlighter-rouge">GetAsyncEnumerator()</code> is invoked on this wrapper, it calls <code class="language-plaintext highlighter-rouge">GetAsyncEnumerator(token)</code> on the underlying enumerable.</p>

<h3 id="appendix-relevant-interfaces">Appendix: relevant interfaces</h3>

<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">using</span> <span class="nn">System.Threading</span><span class="p">;</span>

<span class="k">namespace</span> <span class="nn">System.Collections.Generic</span>
<span class="p">{</span>
    <span class="c1">/// &lt;summary&gt;Exposes an enumerator that provides asynchronous iteration over values of a specified type.&lt;/summary&gt;</span>
    <span class="c1">/// &lt;typeparam name="T"&gt;The type of values to enumerate.&lt;/typeparam&gt;</span>
    <span class="k">public</span> <span class="k">interface</span> <span class="nc">IAsyncEnumerable</span><span class="p">&lt;</span><span class="k">out</span> <span class="n">T</span><span class="p">&gt;</span>
    <span class="p">{</span>
        <span class="c1">/// &lt;summary&gt;Returns an enumerator that iterates asynchronously through the collection.&lt;/summary&gt;</span>
        <span class="c1">/// &lt;param name="cancellationToken"&gt;A &lt;see cref="CancellationToken"/&gt; that may be used to cancel the asynchronous iteration.&lt;/param&gt;</span>
        <span class="c1">/// &lt;returns&gt;An enumerator that can be used to iterate asynchronously through the collection.&lt;/returns&gt;</span>
        <span class="n">IAsyncEnumerator</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;</span> <span class="nf">GetAsyncEnumerator</span><span class="p">(</span><span class="n">CancellationToken</span> <span class="n">cancellationToken</span> <span class="p">=</span> <span class="k">default</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="c1">/// &lt;summary&gt;Supports a simple asynchronous iteration over a generic collection.&lt;/summary&gt;</span>
    <span class="c1">/// &lt;typeparam name="T"&gt;The type of objects to enumerate.&lt;/typeparam&gt;</span>
    <span class="k">public</span> <span class="k">interface</span> <span class="nc">IAsyncEnumerator</span><span class="p">&lt;</span><span class="k">out</span> <span class="n">T</span><span class="p">&gt;</span> <span class="p">:</span> <span class="n">IAsyncDisposable</span>
    <span class="p">{</span>
        <span class="c1">/// &lt;summary&gt;Advances the enumerator asynchronously to the next element of the collection.&lt;/summary&gt;</span>
        <span class="c1">/// &lt;returns&gt;</span>
        <span class="c1">/// A &lt;see cref="ValueTask{Boolean}"/&gt; that will complete with a result of &lt;c&gt;true&lt;/c&gt; if the enumerator</span>
        <span class="c1">/// was successfully advanced to the next element, or &lt;c&gt;false&lt;/c&gt; if the enumerator has passed the end</span>
        <span class="c1">/// of the collection.</span>
        <span class="c1">/// &lt;/returns&gt;</span>
        <span class="n">ValueTask</span><span class="p">&lt;</span><span class="kt">bool</span><span class="p">&gt;</span> <span class="nf">MoveNextAsync</span><span class="p">();</span>

        <span class="c1">/// &lt;summary&gt;Gets the element in the collection at the current position of the enumerator.&lt;/summary&gt;</span>
        <span class="n">T</span> <span class="n">Current</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="p">}</span>
    <span class="p">}</span>
    
    <span class="c1">/// &lt;summary&gt;Provides a mechanism for releasing unmanaged resources asynchronously.&lt;/summary&gt;</span>
    <span class="k">public</span> <span class="k">interface</span> <span class="nc">IAsyncDisposable</span>
    <span class="p">{</span>
        <span class="c1">/// &lt;summary&gt;</span>
        <span class="c1">/// Performs application-defined tasks associated with freeing, releasing, or</span>
        <span class="c1">/// resetting unmanaged resources asynchronously.</span>
        <span class="c1">/// &lt;/summary&gt;</span>
        <span class="n">ValueTask</span> <span class="nf">DisposeAsync</span><span class="p">();</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Original code for <a href="https://github.com/dotnet/corefx/blob/master/src/Common/src/CoreLib/System/Collections/Generic/IAsyncEnumerable.cs">IAsyncEnumerable</a>, <a href="https://github.com/dotnet/corefx/blob/master/src/Common/src/CoreLib/System/Collections/Generic/IAsyncEnumerator.cs">IAsyncEnumerator</a> and <a href="https://github.com/dotnet/corefx/blob/master/src/Common/src/CoreLib/System/IAsyncDisposable.cs">IAsyncDisposable</a>.</p>

<p>For further details, see the <a href="https://github.com/dotnet/roslyn/blob/master/docs/features/async-streams.md">async-streams design doc</a>.</p>
]]></description>
      <guid isPermaLink="true">https://blog.monstuff.com/archives/2019/03/async-enumerables-with-cancellation.html</guid>
      <pubDate>2019-03-08T00:00:00-08:00</pubDate>
    </item>
    <item>
      <title>Using C# 7.1</title>
      <link>/archives/2017/07/using-Csharp-7.1.html</link>
      <description><![CDATA[<p>C# 7.0 was released as part of Visual Studio 2017 (version 15.0). While we work on C# 8.0, we will also ship features that are ready earlier as point releases.</p>

<p>C# 7.1 is the first such release. It will ship along with Visual Studio 2017 version 15.3. To try it out today, you can <a href="https://www.visualstudio.com/vs/preview/">install Visual Studio Preview</a> side-by-side, quickly and safely.</p>

<p>As you start using new C# 7.1 features in your code, a lightbulb will offer you to upgrade your project, either to “C# 7.1” or “latest”. If you leave your project’s language version set to “default”, you can only use C# 7.0 features (“default” means the latest major version, so does not include point releases).</p>

<p>Note: make sure you select <strong>Configuration</strong> <code class="language-plaintext highlighter-rouge">All Configuration</code>, as <code class="language-plaintext highlighter-rouge">Debug</code> is the configuration selected by default when editting a project.</p>

<p><img src="/archives/images/LangVer7_1.png" alt="LangVer7_1.png" /></p>

<p>Here are more specific instructions for using C# 7.1 in <a href="https://github.com/dotnet/roslyn/issues/18783#issuecomment-308510444">ASP.NET and ASP.NET Core</a> and <a href="https://github.com/dotnet/roslyn/issues/18783#issuecomment-308516907">.NET CLI</a>. The NuGet compiler packages for this release are <a href="https://github.com/dotnet/roslyn/wiki/NuGet-packages#versioning">versioned</a> 2.3.</p>

<p>You can provide feedback on the C# features on the <a href="https://github.com/dotnet/roslyn/issues/new">Roslyn repository</a> or via the “Report a Problem” button in Visual Studio.</p>

<h1 id="c-71-features">C# 7.1 features</h1>
<p>In addition to <a href="https://github.com/dotnet/roslyn/issues?q=is%3Aissue+milestone%3A15.3+label%3AArea-Compilers+is%3Aclosed">numerous issues</a> fixed in this release, the compiler comes with the following features for C# 7.1 (summarized below): async Main, pattern-matching with generics, “default” expressions, and inferred tuple names.</p>

<p>You can find more details about C# 7.1 and our progress on C# 7.2 and 8.0 in the <a href="https://github.com/dotnet/roslyn/blob/master/docs/Language%20Feature%20Status.md">language feature status page</a>.</p>

<h2 id="async-main">Async Main</h2>
<p>This makes it easier to get started with async code, by recognizing <code class="language-plaintext highlighter-rouge">static async Task Main() {...await some asynchronous code...}</code> as a valid entry-point to your program.</p>

<h2 id="pattern-matching-with-generics">Pattern-matching with generics</h2>
<p>This allows using open types in type patterns. For example, <code class="language-plaintext highlighter-rouge">case T t:</code>.</p>

<h2 id="default-literal">“default” literal</h2>
<p>This lets you omit the type in the default operator (<code class="language-plaintext highlighter-rouge">default(T)</code>) when the type can be inferred from the context. For instance, you can invoke <code class="language-plaintext highlighter-rouge">void M(ImmutableArray&lt;int&gt; x)</code> with <code class="language-plaintext highlighter-rouge">M(default)</code>, or specify a default parameter value when declaring <code class="language-plaintext highlighter-rouge">void M(CancellationToken x = default)</code>.</p>

<p><img src="/archives/images/DefaultError.png" alt="DefaultError.png" /></p>

<p><img src="/archives/images/DefaultLightbulb.png" alt="DefaultLightbulb.png" /></p>

<h2 id="inferred-tuple-names">Inferred tuple names</h2>
<p>This is a refinement on tuple literals (introduced in 7.0) which makes tuple element names redundant when they can be infered from the expressions.
Instead of writing <code class="language-plaintext highlighter-rouge">var tuple = (a: this.a, b: X.Y.b)</code>, you can simply write <code class="language-plaintext highlighter-rouge">var tuple = (this.a, X.Y.b);</code>. The elements <code class="language-plaintext highlighter-rouge">tuple.a</code> and <code class="language-plaintext highlighter-rouge">tuple.b</code> will still be recognized.</p>

<p><img src="/archives/images/InferredTupleNameLightbulb.png" alt="InferredTupleNameLightbulb.png" /></p>

<h2 id="error-version-pragma">Error version pragma</h2>
<p>This is a small undocumented feature to assist with troubleshooting language version issues. Type <code class="language-plaintext highlighter-rouge">#error version</code> and you will see the version of the compiler that you’re using, as well as your current language version setting.
<img src="/archives/images/ErrorVersion.png" alt="ErrorVersion.png" /></p>

]]></description>
      <guid isPermaLink="true">https://blog.monstuff.com/archives/2017/07/using-Csharp-7.1.html</guid>
      <pubDate>2017-07-09T00:00:00-07:00</pubDate>
    </item>
    </channel>
</rss>
