Func<T, TResult> 委托

Visual Studio 2008
 

命名空间:  System
程序集:  System.Core(在 System.Core.dll 中)

语法

 
 
 
public delegate TResult Func<T, TResult>(
T arg
)
 
J# 支持使用泛型 API,但是不支持新泛型 API 的声明。
 
JScript 不支持泛型类型或方法。

类型参数

T

此委托封装的方法的参数类型。

TResult

此委托封装的方法的返回值类型。

参数

arg
类型:T
此委托封装的方法的参数。 

返回值

类型:TResult
此委托封装的方法的返回值。 

备注

 
 

可以使用此委托表示一种能以参数形式传递的方法,而不用显式声明自定义委托。该方法必须与此委托定义的方法签名相对应。也就是说,封装的方法必须具有一个通过值传递给它的参数,并且必须返回值。

说明:

若要引用具有一个参数并返回 void 的方法(或者要在 Visual Basic 中引用被声明为 Sub 而不是被声明为 Function 的方法),请改用泛型Action<T> 委托。

在使用 Func<T, TResult> 委托时,不必显式定义一个封装只有一个参数的方法的委托。例如,以下代码显式声明了一个名为 ConvertMethod 的委托,并将对 UppercaseString 方法的引用分配给其委托实例。

 
using System;

delegate string ConvertMethod(string inString);

public class DelegateExample
{
public static void Main()
{
// Instantiate delegate to reference UppercaseString method
ConvertMethod convertMeth = UppercaseString;
string name = "Dakota";
// Use delegate instance to call UppercaseString method
Console.WriteLine(convertMeth(name));
} private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
}

以下示例简化了此代码,它所用的方法是实例化 Func<T, TResult> 委托,而不是显式定义一个新委托并将命名方法分配给该委托。

 
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();
}
}

您也可以按照以下示例所演示的那样在 C# 中将 Func<T, TResult> 委托与匿名方法一起使用。(有关匿名方法的简介,请参见匿名方法(C# 编程指南)。)

 
using System;

public class Anonymous
{
public static void Main()
{
Func<string, string> convert = delegate(string s)
{ return s.ToUpper();}; string name = "Dakota";
Console.WriteLine(convert(name));
}
}

您也可以按照以下示例所演示的那样将 lambda 表达式分配给 Func<T, TResult> 委托。(有关 lambda 表达式的简介,请参见 lambda 表达式Lambda 表达式(C# 编程指南)。)

 
using System;

public class LambdaExpression
{
public static void Main()
{
Func<string, string> convert = s => s.ToUpper(); string name = "Dakota";
Console.WriteLine(convert(name));
}
}

Lambda 表达式的基础类型是泛型 Func 委托之一。这样能以参数形式传递 lambda 表达式,而不用显式将其分配给委托。尤其是,因为System.Linq 命名空间中许多类型方法具有 Func<T, TResult> 参数,因此可以给这些方法传递 lambda 表达式,而不用显式实例化 Func<T, TResult> 委托。

示例

 
 

下面的示例演示如何声明和使用 Func<T, TResult> 委托。此示例声明一个 Func<T, TResult> 变量,并为其分配了一个将字符串中的字符转换为大写的 lambda 表达式。随后将封装此方法的委托传递给 Select 方法,以将字符串数组中的字符串更改为大写。

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq; static class Func
{
static void Main(string[] args)
{
// Declare a Func variable and assign a lambda expression to the
// variable. The method takes a string and converts it to uppercase.
Func<string, string> selector = str => str.ToUpper(); // Create an array of strings.
string[] words = { "orange", "apple", "Article", "elephant" };
// Query the array and select strings according to the selector method.
IEnumerable<String> aWords = words.Select(selector); // Output the results to the console.
foreach (String word in aWords)
Console.WriteLine(word);
}
}
/*
This code example produces the following output: ORANGE
APPLE
ARTICLE
ELEPHANT
*/
平台

 
 

Windows Vista, Windows XP SP2, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求

.NET Framework

受以下版本支持:3.5

.NET Compact Framework

受以下版本支持:3.5

Func<T, TResult> 委托的更多相关文章

  1. Action<>和Func<> 委托【代理】

    C#中的Action<>和Func<> 其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的 ...

  2. C#中常见的委托(Func委托、Action委托、Predicate委托)

    今天我要说的是C#中的三种委托方式:Func委托,Action委托,Predicate委托以及这三种委托的常见使用场景. Func,Action,Predicate全面解析 首先来说明Func委托,通 ...

  3. 通过Func 委托理解委托和匿名方法及Lambda 表达式

    Func<T, TResult> 委托 封装一个具有一个参数并返回 TResult 参数指定的类型值的方法. 命名空间: System 程序集: mscorlib(在 mscorlib.d ...

  4. C#内置泛型委托:Func委托

    1.什么是Func委托 Func委托代表有返回类型的委托 2.Func委托定义 查看Func的定义: using System.Runtime.CompilerServices; namespace ...

  5. [C#学习笔记]Func委托与Action委托

    学习一项新知识的时候,最好的方法就是去实践它. 前言 <CLR via C#>这本神书真的是太有意思了!好的我的前言就是这个. Fun 如果要用有输入参数,有返回值的委托,那么Func委托 ...

  6. C# 通过反射调用 Func 委托

    C# 通过反射调用 Func 委托 Intro 最近我的 NPOI 扩展库增加了,自定义输出的功能,可以自定义一个 Func 委托来设置要导出的内容,详细介绍请查看 https://www.cnblo ...

  7. Aap.Net中的Action和Func委托

    前言 最近在阅读某开源框架源码的时候,发现作者在其中运用了很多Action委托和Func委托,虽然我之前在项目中也有一些对委托的实操,但还是免不了长时间的不用,当初消化的一些委托基础都遗忘了...索性 ...

  8. C#扫盲篇(三):Action和Func委托--实话实说

    一.基础定义 老王想找老张的老婆出去耍,但是一看,老张还在厨房煮饭.于是老王就对老张隔壁的淑芬说:"等下老张吃完饭出去喝茶,你就把前门晒的苞谷收了,老张从左门出,你就收右边的苞谷,我就知道从 ...

  9. Func委托与表达式树Expression

    最近在写ORM框架,其中遇到一个难点,就是作为框架调用方如何将查询条件传入框架内.其中就用到了Expression. Func委托 要Expression先要了解Func委托,Func委托的样式是: ...

  10. winform总结2> Action<> ,Action,func<>,委托相关的理解

    1,他们是什么: Action 封装一个方法,该方法不具有参数并且不返回值. Action<> 封装一个方法,该方法具有最多16个参数并且不返回值. func<> 封装一个具有 ...

随机推荐

  1. ifup/ifdown ethX 和 ifconfig ehtX up/down的区别

    相同点:[启用]和[禁止]网卡 ifup  ethX 和 ifconfig  ethX  up               用来启用网卡设备 ifdown  ethX 和 ifconfig  ethX ...

  2. java设计模式——单例模式(三)

    容器单例模式 之前学习Structs2,Spring框架时,经常会听到单例,多例.虽然这与单例模式不太一样,但是都很类似.在程序运行的时候,就加载所有的实例,然后用的时候直接取出 看下面代码: /** ...

  3. Javascript的数据类型和转换

    JavaScript 数据类型 在 JavaScript 中有 5 种不同的数据类型: string number boolean object function 3 种对象类型: Object Da ...

  4. Webpack--模块打包器

    首先介绍一个安装webpack的百度经验:https://jingyan.baidu.com/article/a3a3f811230ee58da3eb8a6e.html 推荐一个详细介绍webpack ...

  5. Everything Be True-freecodecamp算法题目

    Everything Be True 1.要求 完善every函数,如果集合(collection)中的所有对象都存在对应的属性(pre),并且属性(pre)对应的值为真.函数返回ture.反之,返回 ...

  6. 第26题:LeetCode572:Subtree of Another Tree另一个树的子树

    题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 示例 1: ...

  7. 4-8 string

    1.常用的string模块 import string # 26个小写字母 print(string.ascii_lowercase) # abcdefghijklmnopqrstuvwxyz # 2 ...

  8. java.lang.ClassCastException: com.sun.proxy.$Proxy53 cannot be cast to cn.service.impl.WorkinggServiceImpl

    java.lang.ClassCastException: com.sun.proxy.$Proxy53 cannot be cast to cn.service.impl.WorkinggServi ...

  9. Swiper.js手动滑动之后,不再自动滑动问题

    var swiper = new Swiper('.swiper-container', {        pagination: '.swiper-pagination',        autop ...

  10. JZOJ 5775. 【NOIP2008模拟】农夫约的假期

    5775. [NOIP2008模拟]农夫约的假期 (File IO): input:shuru.in output:shuru.out Time Limits: 1000 ms  Memory Lim ...