473,662 Members | 2,622 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IEnumerator<T> and IDisposable: what to dispose?

I have a feeling this is a dumb one, but here it is:

IEnumerator<Tim plements IDisposable, but the thing is I'm not sure
what I'm supposed to dispose! I'm also curious why IEnumerator<T>
implements IDisposable, but not IEnumerator.

Anyone?

Thanks,
HC
Jul 2 '08 #1
2 11770
Hi

To have possibility to safely enumerate resource-based data, such as DataReader,
on common scenarious (like foreach). On other cases empty method does not
heavily implact on performance.

Regards, Alex Meleta
mailto:am*****@ gmail.com; blog:devkids.bl ogspot.com
Jul 3 '08 #2
On Jul 3, 12:07*am, Henri.Chinas... @googlemail.com wrote:
I have a feeling this is a dumb one, but here it is:

IEnumerator<Tim plements IDisposable, but the thing is I'm not sure
what I'm supposed to dispose!
It's very rare that you need to call Dispose yourself on an
IEnumerator<T>. It's more likely that you'll be using IEnumerable<T>
and the foreach statement. foreach automatically calls Dispose.
I'm also curious why IEnumerator<T>
implements IDisposable, but not IEnumerator.
An oversight, in my view. foreach already called Dispose if the
IEnumerator actually implemented IDisposable - but now the generic
version always does, which makes life simpler.

It allows iterators to work with resources which need cleaning up. As
an example, I have a LineReader class which implements
IEnumerable<str ing>. You construct it with a filename (or a delegate
which returns a TextReader when called) and when you start iterating,
it opens the file (or calls the delegate). However, if the caller
decides to stop iterating at some point before LineReader reaches the
end of the file, we still want to close it. The fact that foreach
calls Dispose makes this easy to achieve.

(This is the way that finally blocks work in iterator blocks in C# 2
and 3, by the way.)

Jon
Jul 3 '08 #3

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

Similar topics

10
2744
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 'System.Collections.IEnumerable.GetEnumerator()'. 'HelloWorld.A.GetEnumerator()' is either static, not public, or has the wrong return type. class A : IEnumerable<string>
3
6409
by: Adam Clauss | last post by:
I am developing an abstract class which implements IEnumerable<T>. I need the actual implemented methods here to be abstract as well - they will be implemented by MY subclasses. However, I cannot seem to get the explicit interface method to accept being abstract. Take the following simple class: abstract class TestEnumerable : IEnumerable<string>
1
3696
by: midnight madness | last post by:
I tried but failed to implement a template class that support IEnumerator<T> interface using C++/CLI in VS 2005 Professional version. I could not figure out the proper syntax to implement the property "Current". The challange is that I need to implement two versions of the property: one of type System::Object as required by Collections::IEnumerator, the other of type T^ as required by Collections::Generic::IEnumerator<T> ...
6
5995
by: nicolas.rolland | last post by:
Would anyone know the reson why IList<Tdoes not implements IList ?? This results in strange behaviours, like typeof(IList).IsAssignableFrom(typeof(List<string>)) --true typeof(IList).IsAssignableFrom(typeof(IList<string>)) --false
15
2724
by: Gustaf | last post by:
Using VS 2005. I got an 'IpForm' class and an 'IpFormCollection' class, containing IpForm objects. To iterate through IpFrom objects with foreach, the class is implemented as such: public class IpFormCollection : IEnumerable<IpForm> { ArrayList forms = new ArrayList(); public IEnumerator<IpFormGetEnumerator() {
0
1488
by: emma_middlebrook | last post by:
Hi Hopefully the title is quite accurate but here's some more information. I have a load of ICollection<references hanging off a class e.g. ICollection<X>, ICollection<Yetc etc. Each of the collection's types are all derived from a base class Base i.e X : Base, Y : Base etc etc.
5
9372
by: Shikari Shambu | last post by:
Hi, I am trying to implement a collection that implements IEnumerable<T>. I keep getting the following error 'IEnumerator<...>.Current' in explicit interface declaration is not a member of interface. Please help me resolve the error I was able to implement the non Generics version
9
7878
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private List<as a property. Consider the example below, the class "ListTest" contains a private "List<>" called "strings" - it also provides a public method to add to that list,
2
19859
by: Steve Richter | last post by:
very confused on how to implement the IEnumerable and IEnumerator interfaces on a generic type. I understand I should just use a foreach loop in the GetEnumerator method and use "yield return", etc. would like to know how implement my generic GetEnumerator to yield return an interface to IEnumerator<T>. I am getting a compile error that reminds me a lot of C++ template programming. thanks,
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.