473,396 Members | 1,789 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,396 software developers and data experts.

IEnumerable semantics

Hi all,

I am asking myself wheter IEnumerable has any inherent semantics? The
docs seem to be very unclear to me.

More specifically: When using a "foreach(...)" construct to enumerate a
Stack, Queue oder ArrayList using the IEnumerable interface - can I
safely make any assumptions about the order in which the elements are
returned? That is, LIFO for a Stack, FIFO for a queue and an order
corresponding to the indices when using an ArrayList?

Or does IEnumerable just guarantee that it will enumerate all elements
(with no specific, i. e. implementation-dependant order)?

TIA,
Matthias

Nov 17 '05 #1
3 1715
On 24 Sep 2005 15:20:48 -0700, "el************@web.de"
<el************@web.de> wrote:
Hi all,

I am asking myself wheter IEnumerable has any inherent semantics? The
docs seem to be very unclear to me.

More specifically: When using a "foreach(...)" construct to enumerate a
Stack, Queue oder ArrayList using the IEnumerable interface - can I
safely make any assumptions about the order in which the elements are
returned? That is, LIFO for a Stack, FIFO for a queue and an order
corresponding to the indices when using an ArrayList?


IEnumerable itself is just an interface that allows iteration through
a list of items. Any implementation of IEnumerable is free to select
their own order. Hashtable implements IEnumerable even though it is
completly unordered.

Unless the documentation for a specific implementation says anything
specific about the order you can't be guaranteed anything.

I would however be very surprised if ArrayList returned items in any
other order than from first to last. Queue and Stack is another matter
though, since neither of those have any concept of first and last
element.

--
Marcus Andrén
Nov 17 '05 #2
I have some methods that perform operations where the result is a
particular order of objects. These methods make use of Stack or Queue,
but that is an implementation detail. The correct resulting order is
determined by dequeuing or popping, but I would like to hide that from
the caller.

Now I asked myself if returning the IEnumerable provided by whatever
implementing collection type I choose to use is sufficient, or if I
should copy the "correct" order to an ArrayList and return the
IEnumerable of that?

Or even return that ArrayList itself - interestingly, even for
ArrayList there seem to be no guarantees about the order:
http://msdn.microsoft.com/library/en...ratorTopic.asp

(Ok, I could still write my own implementation of IEnumerable that can
be initialized with a Stack or Queue or ArrayList and would make sure
by calling pop, dequeue or whatever that the order is that way I want
it to be... but hey, programmers should be lazy, shouldn't they ;)?)

Thanks,
Matthias

Nov 17 '05 #3
Well, one more aspect:

IEnumerator types should not modify the underlying collection, as
several clients are allowed to use different enumerators to iterate
through the same collection concurrently. This works as long as the
collection is not modified.

Thus, the enumerator must not be implemented in a way that makes use of
pop(), dequeue() etc on the underlying collection. Now that prevents me
from writing suitable enumerators myself.

Moreover, iterating through a collection requires knowledge on how to
access the "next" element (2nd one from the stack top, queue front),
but that is clearly up to the implementation of the collection.

So the collection implementor is the only one who can guarantee the
enumerator to work in a certain way.

Although ArrayList, Queue and Stack *seem to* behave as expected when
enumerating, this is clearly something that should be addressed by the
specification.

Opinions?

Matthias

Nov 17 '05 #4

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

Similar topics

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...
3
by: KH | last post by:
I can't seem to figure this one out... I've searched MSDN and Goog, and made my best guesses to no avail,, so help would be much appreciated! public ref class T sealed : public...
5
by: Tin Gherdanarra | last post by:
Dear mpdls, here is a simple example of an IEnumerable that generates integers: It works, but I have only a vague idea of what's going on. I understand that /yield/ wraps the humble integer...
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...
2
by: Morgan Cheng | last post by:
In .Net 2.0, Generics are introduced. I found that IEnumerable<T> inherites from IEnumerable. This makes implementation of IEnumerable<Thas to have two GetEnumerator methods defined( one for...
5
by: timor.super | last post by:
Hi group, imagine I want to find number of values of a word in a string ... Look at my code : List<stringthingsToFind = new List<string>(new string {"error", "values"}); MyClass myClass =...
12
by: gnewsgroup | last post by:
I've read the msdn doc about IEnumerable. It seems to me that IEnumerable objects are essentially wrapped-up arrays. It simply gives us the foreach convenience. Is this correct?
2
by: Tony Johansson | last post by:
Hello! Below I have a working program. I have one generic class called Farm<T> with this header definition public class Farm<T: IEnumerable<Twhere T : Animal Now to my question I changed the...
1
by: =?Utf-8?B?UElFQkFMRA==?= | last post by:
Something I found surprising this week involves the IEnumerable<Tinterface. I have a class that I wrote a couple of years ago which implements the IEnumerable interface. This week I realized it...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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,...

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.