【问题标题】:what is this code: Func < T, string > that I don't understand这段代码是什么:我不明白的 Func < T, string >
【发布时间】:2012-02-04 08:31:14
【问题描述】:

我有一些我不明白的代码(不是我写的):

private Func<ControllerContext, string> ThemeSelector { get; set; }

所以这不是 type,这是 Func 我不知道这种东西是怎么命名的,但我无法在谷歌上找到一些解释。有人可以给我一些解释给我一个解释的链接吗?

谢谢。

【问题讨论】:

  • 你检查过 MSDN 或 ASP.net 吗?
  • 这可能与此重复:stackoverflow.com/questions/7901609/…
  • @Floradu88: 我试着自己找,但我不知道它是怎么命名的,所以很难搜索......在msdn上我不清楚。

标签: c# asp.net delegates


【解决方案1】:

Func&lt;ControllerContext, string&gt; 是泛型Func&lt;T,K&gt; 的特定类型。 您需要先了解泛型,然后才能完全理解。

因此 ThemeSelector 只是该类型的一个属性,它有 setter 和 getter。

Func 类型是一个委托类型,它表示一个函数,它接受一个类型为 T 的参数并返回一个类型为 K 的实例。

这意味着您可以将任何此类功能分配给该属性。例如:

private string MyFunction(ControllerContext context) {
    return "Hello world!";
}

...

ThemeSelector = MyFunction;

Console.WriteLine(ThemeSelector(null));

将打印“Hello world!”在控制台中。

【讨论】:

  • +1 表示几乎给出了与问题匹配的示例的代码
  • 谢谢你们,这一篇与我最相关。
【解决方案2】:

这是一个委托类型,即这是一个属性,可以设置。并且可以作为方法调用。

例如

/* Action<String> is a delegate, get it as an object*/
public void Foo (String[] arr, Action<String> instruction) {
    foreach (var element in arr) {
        //use it as a method
        instruction(element);
    }
}

编辑

Func 也是如此

// Func<Int32, Boolean> means that this is a function which accepts one argument as an integer and returns boolean.
public void Foo(Int32 data, Int32 otherData, Func<Int32, Boolean> instruction) {
    var result1 = instruction(data);
    var result2 = instruction(data2);
    Console.WriteLine(result1==result2);
}

编辑

更全面的示例。

//This methods accepts the third parameter as a function with 2 input arguments of type
// Int32 and return type String
public static void DoSomething(Int32 data1, Int32 data2, Func<Int32, Int32, String> whatToDo) {
    var result = whatToDo(data1, data2);
    Console.WriteLine(result);
}

public static Main(String[] args) {
    // here we are calling the DoSomething and
    // passing the Concat method as if it was an object.
    DoSomething(1, 2, Concat);
    // The same with multiply concat.
    DoSomething(7, 2, MultiplyConcat);
}
public static String Concat(Int32 arg1, Int32 arg2) {
    return arg1.ToString() + " " + arg2.ToString():
}
public static String MultiplyConcat(Int32 arg1, Int32 arg2) {
    return (arg1 * arg2).ToString();
}

【讨论】:

    【解决方案3】:

    这是一个类型!实际上它是一个 delegate 类型。它表示一种无需显式声明委托即可使用的方法。

    该类型是泛型类型。您的示例中的一个类型为Func&lt;T, TResult&gt;,它表示一种方法,该方法具有一个 T 类型的参数并返回一个 TResult 类型的值。具体到您的示例,它表示具有ControllerContext 参数并返回string 值的方法:

    string ThemeSelectorHandler(ControllerContext context) {
        returns context.ToString();
    }
    

    进一步阅读:

    【讨论】:

      【解决方案4】:

      *委托是指向函数的指针。
      *Func 是一个内置的委托类型。尖括号中的最后一个类型是结果类型。
      *继承:对象->委托->函数
      *Action 是内置委托,指向一个没有返回的函数。

      【讨论】:

        猜你喜欢
        • 2020-03-02
        • 2023-04-01
        • 2014-04-11
        • 2019-11-21
        • 2012-09-01
        • 1970-01-01
        • 2014-06-12
        • 2012-05-25
        • 1970-01-01
        相关资源
        最近更新 更多