Why Sum over Nullable return Nullable in same time as Min over real values throw exceptions for empty collections ?<p>This is two related questions:</p> <p>a) Why Sum over&nbsp; int?&nbsp; return nullable int?&nbsp; not a regular ?</p> <p>In this example </p><font size="2"> <p></font><font color="#0000ff" size="2">int</font><font size="2">? a = </font><font color="#0000ff" size="2">null</font><font size="2">;</p> <p></font><font color="#0000ff" size="2">int</font><font size="2">? b = 1;</font></p> <p><font color="#0000ff" size="2">var</font><font size="2"> check = a+b; <font color="#008000">// Result in null</font></p> <p><font color="#0000ff" size="2">int</font><font size="2">?[] numbers = { a, b};</p></font></font><font size="2"> <p><font color="#0000ff" size="2">var</font><font size="2"> numSum = numbers.Sum();&nbsp; <font color="#008000">// Result in 1</font></p> <p></font>Value of numSum&nbsp; is 1 - but value of check is null.&nbsp; </p> <p>What is a reason&nbsp;to return nullable int in case if&nbsp;null-value never expected ?</p> <p>&nbsp;</p> <p>b)&nbsp;&nbsp;Why it was decided to throw EmptySequenceException for some of agreegate operators like a&nbsp; Min(this IEnumerable&lt;long&gt; source)&nbsp; but not for their friends IEnumerable&lt; Nullable&lt;long&gt; &gt;&nbsp; ?</p><font size="2"> <p></font><font color="#0000ff" size="2">int</font><font size="2">?[] numbers = {};</p> <p></font><font color="#0000ff" size="2">var</font><font size="2"> numSum = numbers.Min(); </font><font color="#008000" size="2">// Result in null</p></font><font size="2"> <p></font><font color="#0000ff" size="2">int</font><font size="2">[] numbers2 = {};</p> <p></font><font color="#0000ff" size="2">var</font><font size="2"> numSum2 = numbers2.Min(); </font><font color="#008000" size="2">// Result in costly exception</p></font> <p>Why such a difference ? Why not return Nullable&lt;long&gt; in both situations or throw exception in both ? </p> <p>&nbsp;</p> <p>So - can somebody clarify how it was decided to use Nullable return values for LINQ&nbsp;functions ? </p></font>© 2009 Microsoft Corporation. All rights reserved.Fri, 30 Jan 2009 06:43:37 Zb7ff1857-1b42-4b48-a4c9-850a8219f37bhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/b7ff1857-1b42-4b48-a4c9-850a8219f37b#b7ff1857-1b42-4b48-a4c9-850a8219f37bhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/b7ff1857-1b42-4b48-a4c9-850a8219f37b#b7ff1857-1b42-4b48-a4c9-850a8219f37bTAGhttp://social.msdn.microsoft.com/Profile/en-US/?user=TAGWhy Sum over Nullable return Nullable in same time as Min over real values throw exceptions for empty collections ?<p>This is two related questions:</p> <p>a) Why Sum over&nbsp; int?&nbsp; return nullable int?&nbsp; not a regular ?</p> <p>In this example </p><font size="2"> <p></font><font color="#0000ff" size="2">int</font><font size="2">? a = </font><font color="#0000ff" size="2">null</font><font size="2">;</p> <p></font><font color="#0000ff" size="2">int</font><font size="2">? b = 1;</font></p> <p><font color="#0000ff" size="2">var</font><font size="2"> check = a+b; <font color="#008000">// Result in null</font></p> <p><font color="#0000ff" size="2">int</font><font size="2">?[] numbers = { a, b};</p></font></font><font size="2"> <p><font color="#0000ff" size="2">var</font><font size="2"> numSum = numbers.Sum();&nbsp; <font color="#008000">// Result in 1</font></p> <p></font>Value of numSum&nbsp; is 1 - but value of check is null.&nbsp; </p> <p>What is a reason&nbsp;to return nullable int in case if&nbsp;null-value never expected ?</p> <p>&nbsp;</p> <p>b)&nbsp;&nbsp;Why it was decided to throw EmptySequenceException for some of agreegate operators like a&nbsp; Min(this IEnumerable&lt;long&gt; source)&nbsp; but not for their friends IEnumerable&lt; Nullable&lt;long&gt; &gt;&nbsp; ?</p><font size="2"> <p></font><font color="#0000ff" size="2">int</font><font size="2">?[] numbers = {};</p> <p></font><font color="#0000ff" size="2">var</font><font size="2"> numSum = numbers.Min(); </font><font color="#008000" size="2">// Result in null</p></font><font size="2"> <p></font><font color="#0000ff" size="2">int</font><font size="2">[] numbers2 = {};</p> <p></font><font color="#0000ff" size="2">var</font><font size="2"> numSum2 = numbers2.Min(); </font><font color="#008000" size="2">// Result in costly exception</p></font> <p>Why such a difference ? Why not return Nullable&lt;long&gt; in both situations or throw exception in both ? </p> <p>&nbsp;</p> <p>So - can somebody clarify how it was decided to use Nullable return values for LINQ&nbsp;functions ? </p></font>Sun, 18 Sep 2005 23:47:53 Z2005-09-18T23:47:53Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/b7ff1857-1b42-4b48-a4c9-850a8219f37b#8b4e3be4-4e7f-4be1-a1b9-8214f6ffadf7http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/b7ff1857-1b42-4b48-a4c9-850a8219f37b#8b4e3be4-4e7f-4be1-a1b9-8214f6ffadf7Anders Hejlsberghttp://social.msdn.microsoft.com/Profile/en-US/?user=Anders%20HejlsbergWhy Sum over Nullable return Nullable in same time as Min over real values throw exceptions for empty collections ?<p>Regarding (a), the reason Sum(IEnumerable&lt;int?&gt;)&nbsp;returns int?&nbsp;(and not int) is that the result is null if the source is empty or contains only nulls.<br /><br />Regarding (b),&nbsp;if the source is an enumerable of a non-nullable type,&nbsp;it seems appropriate for the result to also be a non-nullable type.&nbsp;You can always use the Min, Max, or Average method with a lambda argument to produce a sequence of a nullable type and a nullable result. For example:<br /><br />int[] numbers = { 1, 2, 3, 4, 5 };<br />int? minNum = numbers.Min(x =&gt; (int?)x);<br /><br />Anders<br /></p>Tue, 20 Sep 2005 04:06:30 Z2005-09-20T04:06:30Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/b7ff1857-1b42-4b48-a4c9-850a8219f37b#a760c930-ef30-4d4e-b1c3-c560032a5349http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/b7ff1857-1b42-4b48-a4c9-850a8219f37b#a760c930-ef30-4d4e-b1c3-c560032a5349Anders Hejlsberghttp://social.msdn.microsoft.com/Profile/en-US/?user=Anders%20HejlsbergWhy Sum over Nullable return Nullable in same time as Min over real values throw exceptions for empty collections ?Actually, I misspoke on (a) above. Sum ignores null values in the source sequence, but it never returns null. If a sequence is empty or contains only nulls, the result is zero. We could in theory return int from Sum(IEnumerable&lt;int?&gt;), but it would break with the pattern of returning the same type as the element type of the sequence.<br /><br />Anders<br />Tue, 20 Sep 2005 04:40:25 Z2005-09-20T04:40:25Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/b7ff1857-1b42-4b48-a4c9-850a8219f37b#ca1fff27-9a4e-4ddc-ae18-09381f4b1fd2http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/b7ff1857-1b42-4b48-a4c9-850a8219f37b#ca1fff27-9a4e-4ddc-ae18-09381f4b1fd2TAGhttp://social.msdn.microsoft.com/Profile/en-US/?user=TAGWhy Sum over Nullable return Nullable in same time as Min over real values throw exceptions for empty collections ?<p>Hmm..&nbsp; What is a reason for such a filtering ?&nbsp; This is inconsistent - as Sum(a,b) != a + b&nbsp;&nbsp; as well there is no way to figure out if sequence is empty (your trick with&nbsp; x =&gt; (int?)x) does not work - all that we can get is exception or 0 value. <br />Current filtering can easily done with&nbsp;&nbsp; Where(x =&gt; x!=null).Sum().&nbsp;<br /><br />My proposal is to:<br />Sum(IEnumerable&lt;int&gt;&nbsp;x)&nbsp; to return&nbsp; int?&nbsp; , null-value will indicate empty&nbsp;sequence.&nbsp;<br />Sum(IEnumerable&lt;int?&gt; x) to return int?&nbsp; as sum of values or null in case if there no <u>any</u> element or at least <u>one</u> nullable is inside.<br /><br />This will make it possible to use thouse operators inside nested queries. At the moment this is risky as this can result in exceptions. <br /><br />I do not know high-level design goals - so my idea can be flawed. But it's definitely valuable - as current implementations can be obtained easily&nbsp;&nbsp;&nbsp; (int) Sum(IE..&lt;int&gt;)&nbsp; and Where(x =&gt; x!=null).Sum()??0&nbsp; &nbsp;while others options are allowed.&nbsp; </p>Tue, 20 Sep 2005 12:38:08 Z2005-09-20T12:38:08Z