Function reference in a variable?

Say I have a function. I want to add a link to this function in a variable.

Therefore, I could call the function "foo (bool foobar)" from the variable "bar", as if it were a function. EG. 'Bar (Foobar).

How?

+5
source share
4 answers

It looks like you want to save Funcin a variable for later use. Take a look at the examples here :

using System;

public class GenericFunc
{
   public static void Main()
   {
      // Instantiate delegate to reference UppercaseString method
      Func<string, string> convertMethod = UppercaseString;
      string name = "Dakota";
      // Use delegate instance to call UppercaseString method
      Console.WriteLine(convertMethod(name));
   }

   private static string UppercaseString(string inputString)
   {
      return inputString.ToUpper();
   }
}

See how the method UppercaseStringis stored in a variable name convertMethod, which can then be called: convertMethod(name).

+17
source

?

+1

.

, , void. , 15 ( , , ).

, , . Action Func .

Update:

.

0

    void Foo(bool foobar)
    {
/* method implementation */
    }

Action delegate

Public Action<bool> Bar;
Bar = Foo;

;

bool foobar = true;
Bar(foobar);
0

All Articles