Why is the Linq-to-Objects sum of a sequence of nullables itself nullable?
<p>As usual, <code>int?</code> means <code>System.Nullable&lt;int&gt;</code> (or <code>System.Nullable`1[System.Int32]</code>).</p>

<p>Suppose you have an in-memory <code>IEnumerable&lt;int?&gt;</code> (such as a <code>List&lt;int?&gt;</code> for example), let us call it <code>seq</code>; then you can find its sum with:</p>

<pre><code>var seqSum = seq.Sum();
</code></pre>

<p>Of course this goes to the extension method overload <a href="https://msdn.microsoft.com/en-us/library/bb156065.aspx" rel="noreferrer"><code>int? IEnumerable&lt;int?&gt;.Sum()</code> (documentation)</a> which is really a static method on <code>System.Linq.Enumerable</code>.</p>

<p>However, the method never returns <code>null</code>, <strong>so why is the return type declared as <code>Nullable&lt;&gt;</code>?</strong> Even in cases where <code>seq</code> is an empty collection or more generally a collection all of whose elements are the <code>null</code> value of type <code>int?</code>, the <code>Sum</code> method in question still returns zero, not <code>null</code>.</p>

<p>This is evident from the documentation, but also from the System.Core.dll source code:</p>

<pre><code>public static int? Sum(this IEnumerable&lt;int?&gt; source) { 
    if (source == null) throw Error.ArgumentNull("source"); 
    int sum = 0; 
    checked { 
        foreach (int? v in source) { 
            if (v != null) sum += v.GetValueOrDefault(); 
        } 
    } 
    return sum; 
} 
</code></pre>

<p>Note that there is only one <code>return</code> statement and its <em>expression</em> <code>sum</code> has type <code>int</code> (which will then implicitly be converted to <code>int?</code> by a wrapping).</p>

<p>It seems wasteful to always wrap the return value. (The caller could always do the wrapping implicitly on his side if desired.)</p>

<p>Besides, this return type may lead the caller into writing code such as <code>if (!seqSum.HasValue) { /* logic to handle this */ }</code> which will in reality be unreachable (a fact which the C# compiler cannot know of).</p>

<p><strong>So why is this return parameter not simply declared as <code>int</code> with no nullable?</strong></p>

<p>I wonder if there is any benefit of having the same return type as <a href="https://msdn.microsoft.com/en-us/library/bb339977.aspx" rel="noreferrer"><code>int? IQueryable&lt;int?&gt;.Sum()</code></a> (in <code>System.Linq.Queryable</code> class). This latter method may return <code>null</code> in practice if there are LINQ providers (maybe LINQ to SQL?) that implement it so.</p>