Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-28
3
Medium Priority
?
387 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

661 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