2

Consider the following:

public void Step(Action code){}

To reuse this I typically pass a lambda expression through like this:

tr.Step(() => StaticType.SomeMethod(someParameter);

While at other times I can simply pass a void function without using a lambda expression :

tr.Step(SomeNonStaticType.SomeMethod);

Where SomeMethod is :

public override void SomeMethod(){}

Can someone please explain this to me?

EDIT: To be clear both have void return types. EDIT 2: If I'm asking these questions what book should I be reading (in the comments please).

JL.
  • 78,954
  • 126
  • 311
  • 459
  • See: http://stackoverflow.com/questions/3841990/are-there-any-benefits-to-using-a-c-sharp-method-group-if-available – Ani Feb 13 '15 at 12:48
  • `Action` is a delegate; both class methods and lambda expressions can be used as delegates. – Codor Feb 13 '15 at 12:50

1 Answers1

5

Action is a delegate for a method with 0 parameters and no return value.

You can pass any method that meets these criteria to your tr.Step.

You cannot use a method that has parameters or that has a return type as an Action, because the signatures don't match. For that you need to do something like what you're doing there with () => StaticType.SomeMethod(someParameter).

JLRishe
  • 99,490
  • 19
  • 131
  • 169