Currently it is only legal to use types which has a method named
GetEnumerator to be used in a foreach loop.
This makes it impossible to use the same Enumerator after and before a
foreach loop, because GetEnumerator always returns a new one.
My proposal would allow the following scenario:
You could for example the first and the last element of the list as a
special case:
ArrayList l = new ArrayList();
l.Add(1);
l.Add(2);
l.Add(3);
IEnumerator e = l.GetEnumerator ();
if (e.MoveNext())
{
object first = e.current;
// do something with first element
}
// reuse enumerator from above, note that iteration starts with second
// element, not the first, unless we call Reset on the enumerator.
foreach object i in e)
{
// do something
}
object last = e.Current; // current still points to last element
// do something with the last element 3 2006
On Aug 31, 3:51 pm, cody <deutron...@gmx .dewrote:
Currently it is only legal to use types which has a method named
GetEnumerator to be used in a foreach loop.
This makes it impossible to use the same Enumerator after and before a
foreach loop, because GetEnumerator always returns a new one.
Rather than making a language change, why not create your own
IEnumerable implementation which is simply given an IEnumerator which
it always returns?
Note that for first/last special casing, you could use my
SmartEnumerable : http://www.yoda.arachsys.com/csharp/...numerable.html
Jon
Jon Skeet [C# MVP] wrote:
On Aug 31, 3:51 pm, cody <deutron...@gmx .dewrote:
>Currently it is only legal to use types which has a method named GetEnumerato r to be used in a foreach loop. This makes it impossible to use the same Enumerator after and before a foreach loop, because GetEnumerator always returns a new one.
Rather than making a language change, why not create your own
IEnumerable implementation which is simply given an IEnumerator which
it always returns?
Note that for first/last special casing, you could use my
SmartEnumerable : http://www.yoda.arachsys.com/csharp/...numerable.html
Jon
Yes I've read your blog, that's what gave me the idea in the first
place.. :)
Your enumerable is a good idea, but brings performance penalties with it.
One could also invent an operator which yields not the elements but the
indices of a given collection, which also allows one to use the index
within the foreach loop, which could also be used for determining the
first and last element.
Making that GetIndexEnumera tor() an extension method in C# 3 would be
very nice. The more I thing about extension methods the more I love em.
A foreach accepting an IEnumerator would have the advantage that you
could reuse it to save resources (If creating enumerator is a
performance neck e.g. a serverside database cursor has to be opened, or
whatever). I mean, what is Reset() good for when you cannot use it,
unless you do the loop manually, without using foreach?
On Aug 31, 4:31 pm, cody <deutron...@gmx .dewrote:
Yes I've read your blog, that's what gave me the idea in the first
place.. :)
:)
Your enumerable is a good idea, but brings performance penalties with it.
Have you got a situation where those performance penalties are
significant?
In the case of my suggestion of an IEnumerable which just returned the
original IEnumerator, the performance penalty would be absolutely
*tiny*!
One could also invent an operator which yields not the elements but the
indices of a given collection, which also allows one to use the index
within the foreach loop, which could also be used for determining the
first and last element.
Do you mean like Enumerable.Rang e in .NET 3.5? (I'm working on a
generic Range class as part of my book, btw.)
Making that GetIndexEnumera tor() an extension method in C# 3 would be
very nice. The more I thing about extension methods the more I love em.
A foreach accepting an IEnumerator would have the advantage that you
could reuse it to save resources (If creating enumerator is a
performance neck e.g. a serverside database cursor has to be opened, or
whatever). I mean, what is Reset() good for when you cannot use it,
unless you do the loop manually, without using foreach?
If you really, really need the performance, just write the foreach
loop manually.
I suspect that it's *very* rare that the overhead introduced by
something like SmartEnumerable (or the other solution suggested here)
would be significant - definitely not common enough to be worth
requiring a language change.
(As for what Reset is good for - very little, IMO. It's often
unimplemented, and was probably a mistake in hindsight.)
Jon This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: juan |
last post by:
hi i have a teachers class where i want the user to enter a few teachers and
iterate thru them.
In the main i get an error
System.Collections.IEnumerator' does not contain a definition for 'Length'
why is that?
is this approach right? I want to develop an OO App. so is this approach
right?
|
by: Hans |
last post by:
I implemented the IEnumerator interface in some classes to enable the use of the foreach statement. As you probalbly know, the interface asks for the implementation of
object IEnumerator.Curren
bool IEnumerator.MoveNext() an
void IEnumerator.Reset()
The help to IEnumerator.MoveNext describes that "after the end of the collection is passed, subsequent calls to MoverNect return false until reset is called"
When, however, I have two...
|
by: Pavils Jurjans |
last post by:
Hello, please consider the code at the end of this posting, that depicts
simple case of custom enumerator. This particular example will enumerate
over list of random number within 1..10, and will stop once the value of 10
is reached. My question, is what's the real point of implemending the
Reset() method, if it is never used by foreach statement. Of course, it
gives me some allocated place where I can do some "enumerator resetting" and...
|
by: starter |
last post by:
I am trying to learn IEnumerator and collections(ArrayList, Hashtable)
in .NET.
Can anyone tell me what is IEnumerator used for and any online
resources would
be helpful.
Thanks
|
by: Wiktor Zychla [C# MVP] |
last post by:
since generics allow us to implement several IEnumerable<T> interfaces on a
single class, I think that "foreach" should somehow reflect that.
suppose we have a enumerable class
class C : IEnumerable, IEnumerable<int>, IEnumerable<string> { ...
we are allowed to enumerate IEnumerable:
foreach ( object item in o ) ...
| |
by: Leslie Sanford |
last post by:
I've been kicking around an idea mostly as a thought experiment at this
point. The idea is to create a thread safe collection that implements
the IEnumerable interface. The enumerators it creates via the
GetEnumerator method would be synchronized with the collction; if the
collection changes, the existing enumerators are notified via an event.
The accompanying EventArgs derived class object carries information
about the change to the...
|
by: rossum |
last post by:
I have been trying to get the IEnumerable interface to compile, and am
having some difficulties.
When I try to compile:
class TestIEnum : IEnumerable<string{
string theStrings;
public IEnumerator<stringGetEnumerator() {
|
by: cody |
last post by:
Why was this design decision to use this kind of "duck typing" in that case?
All you have to do is have a method GetEnumerator() which returns an
object providing Current and MoveNext(). IEnumerator and IEnumerable are
not necessary. Why is that? And what are these interface good for then?
Just dummy/marker interfaces like Cloneable in java?
|
by: Henri.Chinasque |
last post by:
I have a feeling this is a dumb one, but here it is:
IEnumerator<Timplements 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
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |