Cool Example - Enumerating IEnumerable of Nullable Types - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19) +---- Thread: Cool Example - Enumerating IEnumerable of Nullable Types (/showthread.php?tid=26854) |
Cool Example - Enumerating IEnumerable of Nullable Types - AceInfinity - 08-01-2012 Here's a cool example I put together for somebody who asked if the Current value from the GetEnumerator.MoveNext method returned Null if it was finished Enumerating a collection. The answer to that is no, not a true Null value anyways, Null value in terms of the default for that specific type, but not true null... Unless, of course we're incorporating Nullable types into the equation, which is what I did for him as an example: Code: Dim i As IEnumerable(Of Nullable(Of Integer)) = "012345".Select(Function(x) New Nullable(Of Integer)(Integer.Parse(x))) Check this out, and see that the Current value for the Enumerator does in fact hold Null (true Null), when we finish our Enumeration past the last element in the collection of IEnumerable. Take this example into consideration now, and see that it's not Null, but the default 'Nothing' value for Integer... 0... : Code: Dim i As IEnumerable(Of Integer) = "012345".Select(Function(x) Integer.Parse(x)) C# Version: Code: IEnumerable<int> i = "012345".Select(x => int.Parse(x.ToString())); Nullable types C# Version: Code: IEnumerable<int?> i = "012345".Select(c => new int?(int.Parse(c.ToString()))); RE: Cool Example - Enumerating IEnumerable of Nullable Types - HF~Legend - 08-04-2012 Good idea ! Thanks for sharing RE: Cool Example - Enumerating IEnumerable of Nullable Types - AceInfinity - 08-04-2012 (08-04-2012, 03:38 PM)HF~Legend Wrote: Good idea ! Thanks for sharing It wasn't an idea snippet per say as much as it was a demonstration to show what the return value was when the Enumerator reaches the end of it's collection. RE: Cool Example - Enumerating IEnumerable of Nullable Types - Cubs - 08-05-2012 Great demonstration! i hope to see more, keep up the good work! RE: Cool Example - Enumerating IEnumerable of Nullable Types - AceInfinity - 08-05-2012 (08-05-2012, 06:31 PM)Cubs Wrote: Great demonstration! Thanks I post lots of code on MSDN so I also enjoy sharing my examples with others. You'll see my username around the MSDN forums in the VB.net and C# sections quite a bit. |