[Mono-dev] Action<T> used

latency latency at gmx.de
Tue Jul 11 03:27:39 EDT 2006


Hi Piotr,

if I'm not mistaken you want to enumerate through all the elements in your 
tree by simple using foreach, but you cant.

In that case you may take a closer look at the IEnumerable and IEnumerator 
Interfaces.

Short explanation:
If an class is marked as IEnumerable it can be used in a foreach statement. 
Because in the statement the compiler accesses the IEnumerator for that 
object. The IEnumerator provides methods to access each and every element in 
your collection. The actual implementation of the IEnumerator depends on how 
your list stores its elements and how you may access them.

Hope I could be of any help.

Kind Regards,
Valentin S.

On Tuesday 11 July 2006 05:06, Piotr Zurek wrote:
> Hi guys,
>
> I have created a simple tree structure using List<T>.
> Below is the metod that I use to count all the nodes in that structure.
>
>         public void CountDescendants()
>         {
>             _totalcount = 0;
>             Queue<Node> todo = new Queue<Node>();
>             todo.Enqueue(this);
>             while (todo.Count > 0)
>             {
>                 Node node = todo.Dequeue();
>                 foreach (Node child in Node.Subnodes) todo.Enqueue(child);
>                 _totalcount++;
>             }
>         }
>
> I have been podering on how to create a simple ForEach method for my
> Node class that would allow me executing different commands on all
> elements of the tree.
> Probably the Action<T> delegate is the way to go here but I ain't got
> a clue how to use it. Any ideas?
>
> Cheers
> Piotr
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list



More information about the Mono-devel-list mailing list