Pass a method as a parameter [duplicate]
Asked Answered
P

6

14

I want to be able to pass a method as a parameter.

eg..

//really dodgy code
public void PassMeAMethod(string text, Method method)
{
  DoSomething(text);
  // call the method
  //method1();
  Foo();
}

public void methodA()
{
  //Do stuff
}


public void methodB()
{
  //Do stuff
}

public void Test()
{
  PassMeAMethod("calling methodA", methodA)
  PassMeAMethod("calling methodB", methodB)
}

How can I do this?

Procreate answered 25/10, 2010 at 14:19 Comment(4)
You should be able to do it with delegates.During
Which version of the .NET framework are you running?Deaf
3.5, can someone show me using the example above? thanksProcreate
Does this answer your question? Pass Method as Parameter using C#Rewarding
H
23

You need to use a delegate, which is a special class that represents a method. You can either define your own delegate or use one of the built in ones, but the signature of the delegate must match the method you want to pass.

Defining your own:

public delegate int MyDelegate(Object a);

This example matches a method that returns an integer and takes an object reference as a parameter.

In your example, both methodA and methodB take no parameters have return void, so we can use the built in Action delegate class.

Here is your example modified:

public void PassMeAMethod(string text, Action method)
{
  DoSomething(text);
  // call the method
  method();    
}

public void methodA()
{
//Do stuff
}


public void methodB()
{
//Do stuff
}

public void Test()
{
//Explicit
PassMeAMethod("calling methodA", new Action(methodA));
//Implicit
PassMeAMethod("calling methodB", methodB);

}

As you can see, you can either use the delegate type explicitly or implicitly, whichever suits you.

Huskamp answered 25/10, 2010 at 14:27 Comment(0)
T
9

Use Action<T>

Example:

public void CallThis(Action x)
{
    x();
}

CallThis(() => { /* code */ });
Tick answered 25/10, 2010 at 14:21 Comment(0)
E
6

Or Func<>

Func<int, string> func1 = (x) => string.Format("string = {0}", x);
PassMeAMethod("text", func1);

public void PassMeAMethod(string text, Func<int, string> func1)
{
  Console.WriteLine( func1.Invoke(5) );
}
Entranceway answered 25/10, 2010 at 14:24 Comment(0)
C
3

Delegates are the language feature that you're going to need to use to accomplish what you're trying to do.

Here's an example using the code you have above (using the Action delegate as a shortcut):

//really dodgy code
public void PassMeAMethod(string text, Action method)
{
    DoSomething(text);
    method(); // call the method using the delegate
    Foo();
}

public void methodA()
{
    Console.WriteLine("Hello World!");
}    

public void methodB()
{
    Console.WriteLine("42!");
}

public void Test()
{
    PassMeAMethod("calling methodA", methodA)
    PassMeAMethod("calling methodB", methodB)
}
Contravention answered 25/10, 2010 at 14:29 Comment(0)
O
0

Building on what BrunoLM did, as that example was brief.

//really dodgy code
public void PassMeAMethod(string text, Action method)
{
  DoSomething(text);
  method();
  Foo();
}

// Elsewhere...

public static void Main(string[] args)
{
    PassMeAMethod("foo", () =>
        {
            // Method definition here.
        }
    );

    // Or, if you have an existing method in your class, I believe this will work
    PassMeAMethod("bar", this.SomeMethodWithNoParams);
}
Ousel answered 25/10, 2010 at 14:27 Comment(1)
Can you use this in a static void?Spoilt
C
0

c# .net2.0 - Let me show a detailed answer for the topic (pass-a-method-as-a-parameter). In my scenario i'm configuring a set of System.Timers.Timer-s, each with a different _Tick method.

delegate void MyAction();

// members
Timer tmr1, tmr2, tmr3;
int tmr1_interval = 4.2, 
    tmr2_interval = 3.5;
    tmr3_interval = 1;


// ctor
public MyClass()
{
  ..
  ConfigTimer(tmr1, tmr1_interval, this.Tmr_Tick);
  ConfigTimer(tmr2, tmr2_interval, (sndr,ev) => { SecondTimer_Tick(sndr,ev); }); 
  ConfigTimer(tmr3, tmr3_interval, new MyAction((sndr,ev) => { Tmr_Tick((sndr,ev); }));
  ..
}

private void ConfigTimer(Timer _tmr, int _interval, MyAction mymethod)
{
  _tmr = new Timer() { Interval =  _interval * 1000 };
  // lambda to 'ElapsedEventHandler' Tick
  _tmr.Elpased += (sndr, ev) => { mymethod(sndr, ev); };
  _tmr.Start();
}

private void Tmr_Tick(object sender, ElapsedEventArgs e)
{
  // cast the sender timer
  ((Timer)sender).Stop();
  /* do your stuff here */
  ((Timer)sender).Start();
}

// Another tick method
private void SecondTimer_Tick(object sender, ElapsedEventArgs e) {..}
Cohette answered 7/6, 2016 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.