For Each compared to Enumerator

  • Thread starter Thread starter Daniel Walzenbach
  • Start date Start date
D

Daniel Walzenbach

Hello,

could somebody please explain me which code sample is superior above the other and why. Or are they practically the same?

' For Each
Dim myArrayList As New System.Collections.ArrayList
Dim myint As System.Int32

For Each myint In myArrayList
' do something
Next

' Enumerator
Dim myEnumerator As IEnumerator
myEnumerator = myArrayList.GetEnumerator

While myEnumerator.MoveNext
' do something
End While

Thank you a lot! I appreciate your efforts.

Daniel Walzenbach
 
they are identical, for each is syntax candy for IEnumerator (imagine if you had to code the IEnumerator code each time you wanted this functionality!). it makes your life much easier!

hth
jayson
 
Hello Daniel,

I agree with Jayson's reply. In addition, I believe "For Each" syntax is
easier to read and write.

Please feel free to let me know if any further is needed.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Daniel,
As the others have suggested they are both "the same", however the For Each
includes a Try/Catch block so as to ensure that the Dispose method of the
Enumerator object is called.

Its important to call the Dispose method of the Enumerator as you may be
using Win32 APIs to walk an unmanaged resource...

In addition to the Dispose I like the For Each as its "cleaner", especially
when you consider that VB.NET 2004 allows you to include the control
variable on the statement itself.

Dim myArrayList As New System.Collections.ArrayList

For Each myint As System.Int32 In myArrayList
' do something
Next

You can use ILDASM.EXE to look at the IL that is created.

Hope this helps
Jay

message Hello,

could somebody please explain me which code sample is superior above the
other and why. Or are they practically the same?

' For Each
Dim myArrayList As New System.Collections.ArrayList
Dim myint As System.Int32

For Each myint In myArrayList
' do something
Next

' Enumerator
Dim myEnumerator As IEnumerator
myEnumerator = myArrayList.GetEnumerator

While myEnumerator.MoveNext
' do something
End While

Thank you a lot! I appreciate your efforts.

Daniel Walzenbach
 
Why is it so important to call Dispose? Isn't it going to get called when
the enumerator gets GCd? I never understood why all .Net articles put so
much emphasis on releasing resources, if you wanted to have control over
that you would go with unmanaged code, wouldn't you?

Jerry
 
Objects from classes that implement Dispose do so becuase they have (or
could have) unmanaged resources. Remember that there are sometimes rather
small limits to the number of OS handles for certain types of system
resources. If your program runs through enough without closing them, the
system will be unable to allocate more and you may crash. Another example is
database connections. You REALLY don't want more than one open connection to
a database from any given user context if you can help it. Even if you are
multiplexing connections, you may eventually run out of available
connections on the connection pool, or worse yet, available connections on
the server itself.

-Rob Teixeira [MVP]
 
Jerry III said:
Why is it so important to call Dispose? Isn't it going to get called when
the enumerator gets GCd? I never understood why all .Net articles put so
much emphasis on releasing resources, if you wanted to have control over
that you would go with unmanaged code, wouldn't you?

When you can't read the file that you've just written because you
haven't closed the FileStream which was writing it, you'll see why :)

Basically most unmanaged resources are fairly critical in terms of time
of release. Memory is the one resource which is sufficiently uniform
that it can be adequately handled using GC. You really don't want GUI
handles, network connections, files etc to be open until the GC happens
to next run.
 
Jerry,
As the other haves suggested, to release any unmanaged resource, which may
either lock your out in Jon's file case. Or there may be a limited number
available, such as database connections.

if you wanted to have control over
that you would go with unmanaged code, wouldn't you?
Are you suggesting that the entire program be writing in unmanaged code? Of
course that is an option, for you maybe. Personally I prefer all the
benefits that managed code gives me! Hence I can live with "limited resource
management" (Dispose & Close methods).

Hope this helps
Jay
 
Back
Top