﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Patrik commented on Checking For Empty Enumerations</title><description>@Eduard
  
  
You're right, I didn't read the code in detail. I didn't catch the static variable. This is clearly a case where the implementation of the method that returns the IEnumerable is faulty, not the implementation of yield. It's still broken though in other ways.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment36</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment36</guid><pubDate>Sat, 26 Jun 2010 21:47:13 GMT</pubDate></item><item><title>SinsI commented on Checking For Empty Enumerations</title><description>1) You don't need to check for null - if program fails with that, it means there is an error that requires your immediate attention - so it SHOULD stop execution . Otherwise, you can always catch the resulting exception.
  
2) Why are there no  .IsEmpty member in IEnumerable? Why must we try to re-invent the wheel?
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment35</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment35</guid><pubDate>Sat, 26 Jun 2010 08:52:44 GMT</pubDate></item><item><title>Eduard Tom&amp;#224;s commented on Checking For Empty Enumerations</title><description>@Patrik
  
I disagree... the @hazzik problem is about using a static variable, not yield...
  
Yes you can build non-valid IEnumerables with yield, but that does not mean that yield was broken :)
  
  
Greetings
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment34</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment34</guid><pubDate>Sun, 20 Jun 2010 12:02:53 GMT</pubDate></item><item><title>Patrik commented on Checking For Empty Enumerations</title><description>@hazzik That's just one more way in which the c# implementation of IEnumerable and IEnumerator with the yield-keyword is broken.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment33</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment33</guid><pubDate>Fri, 18 Jun 2010 09:37:13 GMT</pubDate></item><item><title>hazzik commented on Checking For Empty Enumerations</title><description>In following sample booth implementation with self.Any()==false and self.Count() = 0 loses data. Any lose only first element, and Count lose all of it.
  
  
        static void Main(string[] args)
  
        {
  
            var enumerable = Enumerable();
  
  
            if (enumerable.IsNullOrEmpty())
  
            {
  
                Console.WriteLine("enumerable is empty");
  
            }
  
            else
  
            {
  
                foreach (var i in enumerable)
  
                    Console.WriteLine(i);
  
            }
  
            Console.ReadKey();
  
        }
  
  
        private static int item;
  
  
        private static IEnumerable
&lt;int Enumerable()
  
        {
  
            while (item &lt; 10)
  
                yield return item++;
  
        }
&gt;</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment32</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment32</guid><pubDate>Thu, 17 Jun 2010 16:53:11 GMT</pubDate></item><item><title>Lucas O. commented on Checking For Empty Enumerations</title><description>Enumerables can be iterated just once. 
  
  
It is not the same thing use the same enumerator instance that use 2 different instances. Remember that we cannot modify the iterated collection when we use enumerator, in fact if we do that, the next call to MoveNext() method throws an exception. 
  
  
We can get so many as we want to enumerators but between 
  
its creations, lot of things can happen. In the Oren's example, he use two differents enumerators but between them can be a line deleting all the files. That is the problem. 
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment31</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment31</guid><pubDate>Mon, 14 Jun 2010 15:07:18 GMT</pubDate></item><item><title>Dhananjay Goyani commented on Checking For Empty Enumerations</title><description>+1 for Phil. Any() doesn't iterate next onto passed enumerator. Also Oren's solution doesn't seem to be good one.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment30</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment30</guid><pubDate>Mon, 14 Jun 2010 07:51:29 GMT</pubDate></item><item><title>mike commented on Checking For Empty Enumerations</title><description>Wow, that is some tricky code
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment29</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment29</guid><pubDate>Fri, 11 Jun 2010 20:25:41 GMT</pubDate></item><item><title>Erlis Vidal commented on Checking For Empty Enumerations</title><description>Hi Luis, 
  
  
I totally agree, is damn confussing. It should be a Helper. 
  
  
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment28</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment28</guid><pubDate>Fri, 11 Jun 2010 16:27:20 GMT</pubDate></item><item><title>Luis Abreu commented on Checking For Empty Enumerations</title><description>Guys, can't believe this. 
  
  
X x = null;
  
x.DoSomething();
  
  
what's the expected result? null reference exception. Why are these extension methods behaving differently from instance methods? Why aren't they simply helper methods? There's simply no reasoning for keeping this madness...
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment27</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment27</guid><pubDate>Fri, 11 Jun 2010 14:36:38 GMT</pubDate></item><item><title>Paul Irwin commented on Checking For Empty Enumerations</title><description>In your example Ayende, what about IEnumerator.Reset()? Although I agree with previous post about it being IDisposable and should be disposed, which usually would call Reset. Enumerable.Any() in System.Linq does the using pattern to reset the enumerator after it is used.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment26</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment26</guid><pubDate>Fri, 11 Jun 2010 13:13:43 GMT</pubDate></item><item><title>Royston Shufflebotham commented on Checking For Empty Enumerations</title><description>Oren, if you were talking about IEnumerator
&lt;t, you'd be bang on, but with IEnumerable
&lt;t you get a brand new IEnumerator
&lt;t each time. As long as the underlying data doesn't change, repeated calls to IEnumerable
&lt;t.First() would give you the same value each time, as each call gets a new IEnumerator
&lt;t.
  
  
(With Phil H's original post, we really shouldn't have to be checking for null IEnumerable
&lt;t objects - APIs that return null IEnumerables are _really_ broken; you just don't want to have two representations of 'no value', and you can always return Enumerable.Empty
&lt;t() if performance is crazily critical.)
  
&gt;</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment25</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment25</guid><pubDate>Fri, 11 Jun 2010 12:15:00 GMT</pubDate></item><item><title>Bryan Avery commented on Checking For Empty Enumerations</title><description>It works fine for me, sounds like brain thinking before testing a solution.
  
  
The other issue with this is that you are making a copy of the object, lets just hope the object is not too large...!
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment24</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment24</guid><pubDate>Fri, 11 Jun 2010 09:31:57 GMT</pubDate></item><item><title>Robin commented on Checking For Empty Enumerations</title><description>As a side note your implementation is inversed from the name. Or I might be missing something obvious? I. e. the method will return false when the IEnumerable is null or empty which is the opposite of you might expect when calling IsNullOrEmpty().
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment23</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment23</guid><pubDate>Fri, 11 Jun 2010 05:01:11 GMT</pubDate></item><item><title>Haacked commented on Checking For Empty Enumerations</title><description>As many commenters mentioned, the implementation of the Any method gets a new Enumerator which starts at the beginning. So it should be safe to call. I wasn't able to produce your results.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment22</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment22</guid><pubDate>Thu, 10 Jun 2010 21:22:31 GMT</pubDate></item><item><title>Patrik H&amp;#228;gne commented on Checking For Empty Enumerations</title><description>I totally agree with Jason that the semantics are off, but I guess that's a comment to Phils original post, not to this post.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment21</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment21</guid><pubDate>Thu, 10 Jun 2010 14:09:13 GMT</pubDate></item><item><title>Jason commented on Checking For Empty Enumerations</title><description>Marcel,
  
  
Oh I've done it. But I decided against it b/c it just seemed so wrong to have a NULL instance return something from a method call rather than throwing and exception.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment20</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment20</guid><pubDate>Thu, 10 Jun 2010 13:59:15 GMT</pubDate></item><item><title>Marcel Popescu commented on Checking For Empty Enumerations</title><description>[OT] Jason, you mean I'm the only one who wrote an IsNullOrEmpty extension method? "string".IsNullOrEmpty() is part of all my projects :)
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment19</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment19</guid><pubDate>Thu, 10 Jun 2010 13:48:34 GMT</pubDate></item><item><title>Jason commented on Checking For Empty Enumerations</title><description>I don't like the semantics of this method. Calling a method in an instance fashion to check for null should throw a null pointer exception if it's null.
  
  
We don't say "somestring".IsNullOrEmpty, we use String.IsNullOrEmpty(someString)
  
  
Seems counter-intuitive.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment18</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment18</guid><pubDate>Thu, 10 Jun 2010 13:45:02 GMT</pubDate></item><item><title>tobi commented on Checking For Empty Enumerations</title><description>But you are right that this will do side-effects twice although only for one element.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment17</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment17</guid><pubDate>Thu, 10 Jun 2010 13:31:36 GMT</pubDate></item><item><title>tobi commented on Checking For Empty Enumerations</title><description>Ayende, did you actually run the code. I think that Directory.EnumerateFiles(".","*.cs"); returns an IEnumerable. The IsNullOrEmpty method will create ea fresh enumerator from it as well as the foreach loop.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment16</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment16</guid><pubDate>Thu, 10 Jun 2010 13:21:46 GMT</pubDate></item><item><title>Diego Mijelshon commented on Checking For Empty Enumerations</title><description>Oren,
  
  
As Patrick noted, separate enumerations of an enumerable should start at the first element again.
  
  
In fact, the example you gave (Directory.EnumerateFiles) is wrong: it works as expected when doing .Any() first.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment15</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment15</guid><pubDate>Thu, 10 Jun 2010 12:48:27 GMT</pubDate></item><item><title>Patrik H&amp;#228;gne commented on Checking For Empty Enumerations</title><description>My last comment should read:
  
  
An IEnumerable that would return an enumerator that skips the first element the second time GetEnumerator is called would be totally wrong implemented IEnumerable.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment14</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment14</guid><pubDate>Thu, 10 Jun 2010 11:58:16 GMT</pubDate></item><item><title>Patrik H&amp;#228;gne commented on Checking For Empty Enumerations</title><description>LL, I wasn't commenting on your comment. I was commenting on Ayendes blog post.
  
  
An that IEnumerable would return an enumerator that skips the first element the second time GetEnumerator is called would be totally wrong implemented IEnumerable.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment13</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment13</guid><pubDate>Thu, 10 Jun 2010 11:36:55 GMT</pubDate></item><item><title>Morne commented on Checking For Empty Enumerations</title><description>Your example disproves your assertion.
  
  
Would be nice to see an actual example where Phil's IsNullOrEmpty() check will lose the first item.
  
  
Your assertion seems to be based on the assumption that when a foreach contstruct is called on a IEnumerable collection, it will use an existing enumerator for the colleciton if it is available instead of getting a new one. This assumption doesn't seem right to me.
  
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment12</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment12</guid><pubDate>Thu, 10 Jun 2010 11:18:56 GMT</pubDate></item><item><title>LL commented on Checking For Empty Enumerations</title><description>Patrik, below is the structure of IEnumerator
&lt;t, courtesy of Reflector:
  
  
public interface IEnumerator
&lt;t : IDisposable, IEnumerator
  
{
  
    // Properties
  
    T Current { get; }
  
}
&gt;</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment11</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment11</guid><pubDate>Thu, 10 Jun 2010 11:13:42 GMT</pubDate></item><item><title>Patrik H&amp;#228;gne commented on Checking For Empty Enumerations</title><description>That would be a totally wrong implemented IEnumerable. I think you confusing IEnumerable(T) with IEnumerator(T).
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment10</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment10</guid><pubDate>Thu, 10 Jun 2010 11:10:47 GMT</pubDate></item><item><title>LL commented on Checking For Empty Enumerations</title><description>Ayende, enumerators are IDisposable and should be disposed.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment9</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment9</guid><pubDate>Thu, 10 Jun 2010 11:09:23 GMT</pubDate></item><item><title>KAE commented on Checking For Empty Enumerations</title><description>"The first file will never appear here."
  
  
I'm afraid you didn't try to run your code.
  
  
Because it will appear.
  
  
So your point is valid only for some really strange implementation of IEnumerable, that change content based on number of iteration of enumerator that it return through call to GetEnumerator.
</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment8</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment8</guid><pubDate>Thu, 10 Jun 2010 11:09:03 GMT</pubDate></item><item><title>Dennis commented on Checking For Empty Enumerations</title><description>Tom, that will fail with this:
  
  
      private static bool hest = false;
  
      public static IEnumerable
&lt;int GetEnumerator()
  
      {
  
         if (hest)
  
            throw new Exception();
  
         hest = true;
  
         yield return 1;
  
         yield return 2;
  
      }
  
  
An enumerable that only allows 1 iteration
&gt;</description><link>http://ayende.com/4538/checking-for-empty-enumerations#comment7</link><guid>http://ayende.com/4538/checking-for-empty-enumerations#comment7</guid><pubDate>Thu, 10 Jun 2010 10:56:48 GMT</pubDate></item></channel></rss>