473,326 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

IEnumerable for look up a collection.

I have seen the following block of code used by many
people to loop through the items in a collection:

IEnumerator e;
IDisposable disposable1;
Object obj1;
e = SomeCollection.GetEnumerator();
try
{
while (e.MoveNext())
{
obj1 = e.Current;
}
}
finally
{
disposable1 = (e as IDisposable);
if (disposable1 != null)
{
disposable1.Dispose();
}
}

However, whenever I use it the disposable1 is always
null, so I do't see the point of the finally block...
Also, as I read the foreach() loop uses the IEnumerator
to loop through the collection. So my question is: is
there any advantage in using the above code over the
foreach loop?

Thank you!!
Iulian
Nov 15 '05 #1
1 1856
"Iulian Ionescu" <iu************@world-link.net> wrote in message
news:20****************************@phx.gbl...
I have seen the following block of code used by many
people to loop through the items in a collection: .... However, whenever I use it the disposable1 is always
null, so I do't see the point of the finally block...
Also, as I read the foreach() loop uses the IEnumerator
to loop through the collection. So my question is: is
there any advantage in using the above code over the
foreach loop?


The IDisposable is always null because you've only run the loop on
enumerators that don't implement that interface. This form ensures that the
enumerator is disposed as quickly as possible, freeing up any resources it
may hold. Being in a finally block ensures that the resources are freed
even if an exception occurs. There is no significant advantage in using
while over foreach, it's just a matter of preferred style.

--
Gerard John Krupa BSc AMBCS
Purveyor of network management software
http://www.bogbrushzone.co.uk/

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bob Rundle | last post by:
I'm trying to serialize a class with XmlSerializer. This class implements the IEnumerable interface. I implemented the IEnumerable interface for reasons other than Xml serialization. However I...
10
by: jcc | last post by:
Hi guys, I'm a newbie to C#. My Visual Studio 2005 failed to compile the following code with error as 'HelloWorld.A' does not implement interface member...
5
by: Craig Buchanan | last post by:
I have a class called LineItems. It implements IEnumerable. It uses an ArrayList to hold objects. When I try to serialize the object that contains LineItems (an object called Order), I get an...
1
by: cwertman | last post by:
I have an app that I have several functions I need to determine if a given propety is a a type of Collection It could (and in most cases is) a Generic Collection It could be , and array , an...
1
by: =?Utf-8?B?RnJhbmNvaXNWaWxqb2Vu?= | last post by:
Hi there, Does anybody know how to return the results of an IEnumerable<typeas an array of the same type i.e type in a web service call without first having to collect all elements in the...
2
by: =?Utf-8?B?a2VubmV0aEBub3NwYW0ubm9zcGFt?= | last post by:
When creating multiple iterators, the original is defined as returning IEnumerator, ie public IEnumerator GetEnumerator() { yield x; ...} whereas the additional ones are defined as returning...
5
by: GiJeet | last post by:
Hello, I know that all that’s required to use linq to objects, is the collection must implement IEnumerable<Tbut how to know if a collection implements this interface? For example one of the books...
1
by: Ronald S. Cook | last post by:
I bring back a list of Employees as an IEnumerable of Employee... columns include EmployeeId, EmployeeLastName, etc. I want to bind the IEnumerable to a datagrid BUT I want the grid to have an...
1
by: shapper | last post by:
Hello, On my MVC projects I often create classes which contains properties which are lists of other classes. Should I start using IQueryable<Tor IEnumerable<Tinstead of List<T>? What are...
5
by: Anders Borum | last post by:
Hello I'm developing an API with a number of concrete collections that inherit from an abstract collection hierarchy. Each inheritance level provides an implementation of IEnumerable<T>, thus...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.