-2

I Know i m asking a peculiar question but i have to question this because this question is in my mind from long time.

While using Visual Studio as soon as we write "A" while creating new "Actionresult" i.e while we

write 'ActionResult' in public ActionResult YouAction(){...} the first

IntelliSense help which appear is "Action",my question is that what actually is the usage of

"Action" in Asp.Net MVC, in my 10 months career as Asp.Net MVC developer i haven't use it but

looking at its name "Action" i think it is related to MVC only.

Thankzz..!!

  • `Action` and `Action` which appear in the context menu are delegates. And they have nothing in common with the `ActionMethods`, as @AshwiniVerma wrote. – Lukas G Aug 01 '14 at 11:26
  • @Ghukas...thankzz..for comment..could be plz explain by answering below.. –  Aug 01 '14 at 11:28
  • Action's are not MVC related, they are part of the .NET framework base classes, and usable in any .net environment (Windows Forms, Console apps, WebForms, MVC, etc..) See this article for info on Actions http://blogs.msdn.com/b/brunoterkaly/archive/2012/03/02/c-delegates-actions-lambdas-keeping-it-super-simple.aspx – Erik Funkenbusch Aug 02 '14 at 15:07

2 Answers2

2

An Action (and it's sibling Action<T>) is a pre-defined delegate type in the .NET framework. It exists to provide a standard interface for function calls that do not return a value. There is a cousin delegate type called Func<TResult> which returns a TResult generic type.

These delegates com in two flavors, generic and non-generic. The non-generic Action takes no parameters, and returns void. The generic Action can take up to 16 parameters of generic types as parameters (but also doesn't return a value)

The same holds true for Func<TResult>, except all versions are generic. It can take 0 to 16 generic parameters and returns a generic type.

In the "old days" to use a delegate, you would have to first define the delegate, then use it. Like this:

public delegate void ShowValue();

Then use it like so...

ShowValue showMethod = testName.DisplayToWindow;
showMethod();

But with an Action, you just have to do this:

Action showMethod = delegate() { testName.DisplayToWindow();} ;
showMethod();

Another nice thing is that anonymous lambda's are by their nature Action or Func<TResult> compatible, so you can just do this:

Action showMethod = () => testName.DisplayToWindow();
showMethod();

This has a lot of nice convenience, but more importantly Microsoft created these delegate types so that their own framework would be able to use a standard interface. You can take advantage of these types in your own code as well.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
1

Action is delegate from System.Action namespace.

MSDN: Encapsulates a method that has a single parameter and does not return a value.

It's not one of MVC's built-in action result types which ActionMethods look for.

  • ViewResult
  • PartialViewResult
  • RedirectResult
  • RedirectToRouteResult
  • ContentResult
  • JsonResult
  • JavaScriptResult
  • FileResult
  • EmptyResult

UPDATED

Now why Action delegate? Please go through this post. Uses of Action delegate in C#

Community
  • 1
  • 1
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56