引用变量中的函数?

Ste*_*nal 6 c# variables types reference function

说我有一个功能.我希望在变量中添加对此函数的引用.

所以我可以从变量'bar'调用函数'foo(bool foobar)',好像它是一个函数.例如.'巴(foobar的)'.

怎么样?

Dav*_*vid 18

听起来你想将a保存Func到一个变量供以后使用.看看这里的例子:

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();
   }
}
Run Code Online (Sandbox Code Playgroud)

看看如何将方法UppercaseString保存到一个被调用的变量中convertMethod,然后可以调用该变量:convertMethod(name).