Solved

Seeking an explanation of what "Action" does in this method?

Posted on 2012-03-28
3
377 Views
Last Modified: 2012-03-28
I'm very new to the Action delegate and have no idea what is going on here. Could someone please explain what could be the purpose of putting "Action" here? Is this somehow related to a "pass by reference"?
public void Traverse(Action<Node> v)
{
   v(this);
   foreach (var c in Children) //Children being a property that returns ReadOnlyCollection containing a List<Node>
   {
      c.Traverse(v);
   }
}

Open in new window

0
Comment
Question by:Eindoofus
  • 2
3 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Action is a type, and it can be thought of like a pointer to a function that has no return value (i.e. returns void). The counterpart to Action for functions which do return values is Func.

Action is delegate type. You can execute an instance of a delegate as if it were an actual function (this is what line 3 in your snippet is doing).

The net effect of your code is that you can pass in the name of any function (within scope) that accepts a parameter of type Node and which returns nothing. Inside of Traverse, this function will be executed at line 3, passing the reference to the current class (which is assumed be Node) to the target function as its parameter.
0
 

Author Comment

by:Eindoofus
Comment Utility
Thanks. Btw, can this be considered a recursive method?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Yes, because it calls itself (line 6).

Also, I just saw the part of your question regarding "pass-by-reference". Action is a class, so it would be pass-by-reference. All classes are passed by reference. Value types (including structs) are passed by value.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now