August 2010 Blog Posts
Wednesday, August 18, 2010 11:58 AM | Comments

A year ago I had written briefly about my love-affair with the yield keyword. Today a question was posed to me and the follow-up discussion was interesting enough to me to warrant a quick post. If you're returning an IEnumberable<T> from a method that reads from a DataReader, is it bad to use yield? My concern is calling a long-running method while looping over the items returned from the method – won’t this keep the SQL connection open longer? Also what if an exception happens while looping, will my reader still get closed? His proposed API was similar to the following very common (and outdated, IMHO) pattern – which is turning a DataReader into data access or business objects. The Proposed API public List<string> GetItemsList() { using (var reader = new FakeDataReader()) { ...