Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-03-28
3
Medium Priority
?
389 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 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 37779930
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
ID: 37779974
Thanks. Btw, can this be considered a recursive method?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37779987
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

885 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