title: Monadic null checking in C# 6.0
body: <p>I stumbled across an interesting site, where some of the new (proposed) features of C# 6.0 are addressed. You may read it here: <a href="http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated" rel="noreferrer">Probable C# 6.0 features</a>.</p>
  
  <p>What I find particular interesting is the monadic null checking (also known as the null-propagation operator <strong>?.</strong>). According to the site, the following statement </p>
  
  <pre><code>var bestValue = points?.FirstOrDefault()?.X ?? -1;
  </code></pre>
  
  <p>contains the monadic null check, which is currently implemented with this piece of code:</p>
  
  <pre><code>if (points != null) 
  {
    var next = points.FirstOrDefault();
    if (next != null &amp;&amp; next.X != null) return next.X;
  }   
  return -1;
  </code></pre>
  
  <p>My first glance was, hey, what the heck is written here? But after looking at the 'old' code, I am starting to like it.</p>
  
  <p>However, I am also starting to get some questions, which I'd like to ask. </p>
  
  <ul>
  <li>I assume that this null-propagating operator is thread safe. But how is that actually performed? Will race conditions be removed or are they persisting?</li>
  <li><p>How would this operator handle generic types? Moreover, how would it deal with unconstrained generic types? For example, consider </p>
  
  <pre><code>var resultAfterNullCheck = x?.Y;
  </code></pre>
  
  <p>If the type Y here is instantiated with reference types, non-nullable value types and nullable value types, there would be nothing reasonable to do (as I cannot think of what to do, as I simply do not know what to do). So is there a default that will be returned? Or will it throw an error?</p></li>
  <li><p>When looking at the example the site provides (and which I copied above) I assume that one of the main benefits of the null-propagation operator will be that it will evaluate the statement only once. However (perhaps due to my lack of knowledge of CLR), I am quite curious on how it could be performed.<br>
  As to me, the first evaluation (if points equals null) should trigger the extension method FirstOrDefault() to trigger when points isn't null, followed by the evalation of the returned type to be null or not, if not, X will be returned. So these are in fact three evaluations combined to one? Or am I understanding it incorrectly? Will this affect speed of execution?</p></li>
  </ul>
  
  <p>In other words, what will be faster, the old way of performing null checks, or this new lovely operator? I will try to examine this by performing some research as soon as the download of Visual Studio 2015 is finished... But that requires a bit of patience...</p>
  
  <p>Are there any thoughts on this new operator type? Is it really still a proposed one, or can we really expect to work with this new monadic null check?</p>
  
  <p><strong>EDIT</strong><br>
  As Matthew Watson provided a nice <a href="http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx" rel="noreferrer">MSDN article</a> discussing this (and more) topic(s), I was curious if it mentioned my earlier question regarding unconstrained generics and how this operator deals with that. Unfortunately, I haven't found an answer yet. While I will suppose that the programmer should try to prevent the usage of unconstraint generics, I can still imagine that this is sometimes not feasible. If that is the case, will a redesign be really necessary?</p>