469,925 Members | 1,492 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,925 developers. It's quick & easy.

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 22728
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

35 posts views Thread by Raymond Hettinger | last post: by
14 posts views Thread by ford_desperado | last post: by
13 posts views Thread by cody | last post: by
8 posts views Thread by cody | last post: by
104 posts views Thread by cody | last post: by
14 posts views Thread by Josh Ferguson | last post: by
15 posts views Thread by Fabio Cannizzo | last post: by
5 posts views Thread by sathyashrayan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.