473,396 Members | 1,693 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.

foreach in reverse direction ? feature for C# V2 ?

Hi

Is this something for C# V2 ?

Queue msgqueue =new Queue ();

//fill some data in the queue here with this function & keep only the last 5
events
void addtolog(string msg)
{
msgqueue.Enqueue(msg);
if (msgqueue.Count>5)
msgqueue.Dequeue();
}

// print contents of queue without dequeing the data.
foreach (object o in msgqueue )
{
Console.WriteLine((string)o);
}

// printing in reverse :
object[] items=msgqueue.ToArray();
for (int i=items.Length;i>0;i-- )
{
Console.WriteLine((string)items[i-1]);
}

Johan
Nov 16 '05 #1
4 22958
Hello!

You could do this with a custom built enumerator. For instance, by
implementing the IEnumerable interface and using the "yield return" keywords
in a reverse loop.

public IEnumerable QueueList
{
object[] items = msgqueue.ToArray();

for (int i = items.Length; i > 0; i--)
{
yield return (string) items[i - 1];
}
}

btw. above is a snippet of code I wrote quickly.. just to illustrate!

--
venlig hilsen / with regards
anders borum (mcp)
--
Nov 16 '05 #2
Hi,

AFAIK there is nothing like that in C# 2.0 if you need it you could use
several workarounds , like using a stack

Stack stack = new Stack ( Queue )

then you can use Pop to get a reverse collection.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Sagaert Johan" <RE*************@hotmail.com> wrote in message
news:OY**************@TK2MSFTNGP14.phx.gbl...
Hi

Is this something for C# V2 ?

Queue msgqueue =new Queue ();

//fill some data in the queue here with this function & keep only the last
5
events
void addtolog(string msg)
{
msgqueue.Enqueue(msg);
if (msgqueue.Count>5)
msgqueue.Dequeue();
}

// print contents of queue without dequeing the data.
foreach (object o in msgqueue )
{
Console.WriteLine((string)o);
}

// printing in reverse :
object[] items=msgqueue.ToArray();
for (int i=items.Length;i>0;i-- )
{
Console.WriteLine((string)items[i-1]);
}

Johan

Nov 16 '05 #3
It would be impossible to implement a reverse foreach as a language
construct (without much overhead), because foreach relies on the IEnumerable
interface, which is supposed to return elements sequentially from start to
end. But in 2.0 you can easily implement a reverse enumerator for objects
implementing IList, like:

class ReverseEnum : IEnumerable
{
IList list;

public RevereseEnum(IList list)
{
this.list = list;
}

IEnumerator GetEnumerator()
{
for (int index = list.Count - 1; index >= 0; index--)
yield return list[index];
}
}

and use it like:

foreach (object o in new RevereseEnum(msgqueue.ToArray()))
{
...
}

HTH,
Stefan

"Sagaert Johan" <RE*************@hotmail.com> wrote in message
news:OY**************@TK2MSFTNGP14.phx.gbl...
Hi

Is this something for C# V2 ?

Queue msgqueue =new Queue ();

//fill some data in the queue here with this function & keep only the last
5
events
void addtolog(string msg)
{
msgqueue.Enqueue(msg);
if (msgqueue.Count>5)
msgqueue.Dequeue();
}

// print contents of queue without dequeing the data.
foreach (object o in msgqueue )
{
Console.WriteLine((string)o);
}

// printing in reverse :
object[] items=msgqueue.ToArray();
for (int i=items.Length;i>0;i-- )
{
Console.WriteLine((string)items[i-1]);
}

Johan

Nov 16 '05 #4
That should have been:

public IEnumerator QueueList instead of
public IEnumerable QueueList.

I'm sorry.

--
venlig hilsen / with regards
anders borum (mcp)
--
Nov 16 '05 #5

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

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
14
by: ford_desperado | last post by:
Why isn't ALLOW REVERSE SCANS the default? Why do we have to - drop PK - create an index - recreate PK What are the advantages of indexes that do not allow reverse scans?
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
13
by: cody | last post by:
foreach does implicitly cast every object in the collection to the specified taget type without warning. Without generics this behaviour had the advantage of less typing for us since casting was...
8
by: cody | last post by:
currently, foreach takes a IEnumerable as parameter, so we can do: foreach (int i in array){} what about an additional form of foreach that takes an IEnumerator as parameter, so we can do: ...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
14
by: Josh Ferguson | last post by:
I don't believe a syntax driven equivalent exists for this, but I just thought it would be neat if you could use foreach to do something like this: foreach (Object x in collection1, collection2)...
15
by: Fabio Cannizzo | last post by:
Is it possible to do something similar to the STL iterators, i.e. to execute a foreach loop in reverse order? Thanks, Fabio
5
by: sathyashrayan | last post by:
Dear group, The function to be used as follows: $links = "http://www.campaignindia.in/feature/analysis"; $tag1 = '<div class=feature-wrapper>'; $tag2 = '<h1><a href'; $tag3 = "</a>"; $op =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.