473,398 Members | 2,404 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,398 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 22959
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...

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.