Func<(Of <(T, TResult>)>) 委托

更新:2007 年 11 月

封装一个具有一个参数并返回 TResult 参数指定的类型值的方法。

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

Visual Basic(声明)
Of T, TResult) ( _
            arg As T _
            ) As TResult
            
Visual Basic (用法)
AddressOf HandlerMethod)
            
C#
public delegate TResult Func<T, TResult>(
            T arg
            )
            
Visual C++
generic<typename T, typename TResult>
            public delegate TResult Func(
            T arg
            )
            
J#
J# 支持使用泛型 API,但是不支持新泛型 API 的声明。
            
JScript
JScript 不支持泛型类型或方法。
            

类型参数

T

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

TResult

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

参数

arg
类型:T

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

返回值

类型:TResult

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

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

Func)>) 委托说明:

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

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

Visual Basic
' Declare a delegate to represent string conversion method
            Delegate Function ConvertMethod(ByVal inString As String) As String
            Module DelegateExample
            Public Sub Main()
            ' Instantiate delegate to reference UppercaseString method
            Dim convertMeth As ConvertMethod = AddressOf UppercaseString
            Dim name As String = "Dakota"
            ' Use delegate instance to call UppercaseString method
            Console.WriteLine(convertMeth(name))
            End Sub
            Private Function UppercaseString(inputString As String) As String
            Return inputString.ToUpper()
            End Function
            End Module
            
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<(Of <(T, TResult>)>) 委托,而不是显式定义一个新委托并将命名方法分配给该委托。

Visual Basic
Module GenericFunc
            Public Sub Main()
            ' Instantiate delegate to reference UppercaseString method
            Dim convertMethod As Func(Of String, String) = AddressOf UppercaseString
            Dim name As String = "Dakota"
            ' Use delegate instance to call UppercaseString method
            Console.WriteLine(convertMethod(name))
            End Sub
            Private Function UppercaseString(inputString As String) As String
            Return inputString.ToUpper()
            End Function
            End Module
            
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<(Of <(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<(Of <(T, TResult>)>) 委托。(有关 lambda 表达式的简介,请参见 Lambda 表达式(C# 编程指南)。)

Visual Basic
Module LambdaExpression
            Public Sub Main()
            Dim convert As Func(Of String, String) = Function(s) s.ToUpper()
            Dim name As String = "Dakota"
            Console.WriteLine(convert(name))
            End Sub
            End Module
            
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 表达式,而不用显式将其分配给委托。尤其是,因为 ) 委托。

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

Visual Basic
Imports System.Collections.Generic
            Imports System.Linq
            Module Func
            Public Sub Main()
            ' Declare a Func variable and assign a lambda expression to the  
            ' variable. The method takes a string and converts it to uppercase.
            Dim selector As Func(Of String, String) = Function(str) str.ToUpper()
            ' Create an array of strings.
            Dim words() As String = { "orange", "apple", "Article", "elephant" }
            ' Query the array and select strings according to the selector method.
            Dim aWords As IEnumerable(Of String) = words.Select(selector)
            ' Output the results to the console.
            For Each word As String In aWords
            Console.WriteLine(word)
            Next
            End Sub
            End Module
            ' This code example produces the following output:
            '           
            '   ORANGE
            '   APPLE
            '   ARTICLE
            '   ELEPHANT
            
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 系统要求

相关文章:

  • 2021-12-06
  • 2021-12-16
  • 2021-05-25
  • 2022-12-23
  • 2021-05-29
  • 2021-08-15
  • 2021-06-21
  • 2021-07-05
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
  • 2022-01-27
  • 2022-02-04
相关资源
相似解决方案