Func<T1,T2,T3,T4,TResult> 委托
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
封装一个具有四个参数的方法,并返回由 TResult 参数指定的类型的值。
generic <typename T1, typename T2, typename T3, typename T4, typename TResult>
public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
public delegate TResult Func<in T1,in T2,in T3,in T4,out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
public delegate TResult Func<in T1,in T2,in T3,in T4,out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4) where T1 : allows ref struct where T2 : allows ref struct where T3 : allows ref struct where T4 : allows ref struct where TResult : allows ref struct;
public delegate TResult Func<T1,T2,T3,T4,TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
type Func<'T1, 'T2, 'T3, 'T4, 'Result> = delegate of 'T1 * 'T2 * 'T3 * 'T4 -> 'Result
Public Delegate Function Func(Of In T1, In T2, In T3, In T4, Out TResult)(arg1 As T1, arg2 As T2, arg3 As T3, arg4 As T4) As TResult
Public Delegate Function Func(Of T1, T2, T3, T4, TResult)(arg1 As T1, arg2 As T2, arg3 As T3, arg4 As T4) As TResult
类型参数
参数
- arg1
- T1
此委托封装的方法的第一个参数。
- arg2
- T2
此委托封装的方法的第二个参数。
- arg3
- T3
此委托封装的方法的第三个参数。
- arg4
- T4
此委托封装的方法的第四个参数。
返回值
此委托封装的方法的返回值。
示例
以下示例演示如何声明和使用 Func<T1,T2,TResult> 委托。 此示例声明一个 Func<T1,T2,TResult> 变量,并为其分配一个 lambda 表达式,该表达式采用一个 String 值和一个 Int32 值作为参数。 如果参数的String长度等于参数的值Int32,则 lambda 表达式将返回true。 封装此方法的委托随后在查询中使用,以筛选字符串数组中的字符串。
using System;
using System.Collections.Generic;
using System.Linq;
public class Func3Example
{
public static void Main()
{
Func<String, int, bool> predicate = (str, index) => str.Length == index;
String[] words = { "orange", "apple", "Article", "elephant", "star", "and" };
IEnumerable<String> aWords = words.Where(predicate).Select(str => str);
foreach (String word in aWords)
Console.WriteLine(word);
}
}
open System
open System.Linq
let predicate = Func<string, int, bool>(fun str index -> str.Length = index)
let words = [ "orange"; "apple"; "Article"; "elephant"; "star"; "and" ]
let aWords = words.Where predicate
for word in aWords do
printfn $"{word}"
Imports System.Collections.Generic
Imports System.Linq
Public Module Func3Example
Public Sub Main()
Dim predicate As Func(Of String, Integer, Boolean) = Function(str, index) str.Length = index
Dim words() As String = { "orange", "apple", "Article", "elephant", "star", "and" }
Dim aWords As IEnumerable(Of String) = words.Where(predicate)
For Each word As String In aWords
Console.WriteLine(word)
Next
End Sub
End Module
注解
可以使用此委托来表示可以作为参数传递的方法,而无需显式声明自定义委托。 封装的方法必须与此委托定义的方法签名相对应。 这意味着封装方法必须具有四个参数,每个参数都按值传递给它,并且必须返回一个值。
Note
若要引用具有四个参数并返回 void(F# 中的 unit)(或Visual Basic中声明为 Sub 的方法,而不是Function),请改用泛型 Action<T1,T2,T3,T4> 委托。
使用 Func<T1,T2,T3,T4,TResult> 委托时,无需显式定义封装具有四个参数的方法的委托。 例如,以下代码显式声明一个命名 Searcher 的泛型委托,并将对方法的 IndexOf 引用分配给其委托实例。
using System;
delegate int Searcher(string searchString, int start, int count,
StringComparison type);
public class DelegateExample
{
public static void Main()
{
string title = "The House of the Seven Gables";
int position = 0;
Searcher finder = title.IndexOf;
do
{
int characters = title.Length - position;
position = finder("the", position, characters,
StringComparison.InvariantCultureIgnoreCase);
if (position >= 0)
{
position++;
Console.WriteLine("'The' found at position {0} in {1}.",
position, title);
}
} while (position > 0);
}
}
open System
type Searcher = delegate of (string * int * int * StringComparison) -> int
let title = "The House of the Seven Gables"
let finder = Searcher title.IndexOf
let mutable position = 0
while position > -1 do
let characters = title.Length - position
position <-
finder.Invoke("the", position, characters, StringComparison.InvariantCultureIgnoreCase)
if position >= 0 then
position <- position + 1
printfn $"'The' found at position {position} in {title}."
Delegate Function Searcher(searchString As String, _
start As Integer, _
count As Integer, _
type As StringComparison) As Integer
Module DelegateExample
Public Sub Main()
Dim title As String = "The House of the Seven Gables"
Dim position As Integer = 0
Dim finder As Searcher = AddressOf title.IndexOf
Do
Dim characters As Integer = title.Length - position
position = finder("the", position, characters, _
StringComparison.InvariantCultureIgnoreCase)
If position >= 0 Then
position += 1
Console.WriteLine("'The' found at position {0} in {1}.", _
position, title)
End If
Loop While position > 0
End Sub
End Module
以下示例通过实例化 Func<T1,T2,T3,T4,TResult> 委托而不是显式定义新委托并向其分配命名方法来简化此代码。
using System;
public class DelegateExample
{
public static void Main()
{
string title = "The House of the Seven Gables";
int position = 0;
Func<string, int, int, StringComparison, int> finder = title.IndexOf;
do
{
int characters = title.Length - position;
position = finder("the", position, characters,
StringComparison.InvariantCultureIgnoreCase);
if (position >= 0)
{
position++;
Console.WriteLine("'The' found at position {0} in {1}.",
position, title);
}
} while (position > 0);
}
}
open System
let indexOf (s: string) s2 pos chars comparison =
s.IndexOf(s2, pos, chars, comparison)
let title = "The House of the Seven Gables"
let finder = Func<string, int, int, StringComparison, int>(indexOf title)
let mutable position = 0
while position > -1 do
let characters = title.Length - position
position <-
finder.Invoke("the", position, characters, StringComparison.InvariantCultureIgnoreCase)
if position >= 0 then
position <- position + 1
printfn $"'The' found at position {position} in {title}."
Module DelegateExample
Public Sub Main()
Dim title As String = "The House of the Seven Gables"
Dim position As Integer = 0
Dim finder As Func(Of String, Integer, Integer, StringComparison, Integer) _
= AddressOf title.IndexOf
Do
Dim characters As Integer = title.Length - position
position = finder("the", position, characters, _
StringComparison.InvariantCultureIgnoreCase)
If position >= 0 Then
position += 1
Console.WriteLine("'The' found at position {0} in {1}.", _
position, title)
End If
Loop While position > 0
End Sub
End Module
可以将委托与 C# 中的匿名方法一 Func<T1,T2,T3,T4,TResult> 起使用,如以下示例所示。 (有关匿名方法的简介,请参阅 匿名方法。
using System;
public class DelegateExample
{
public static void Main()
{
string title = "The House of the Seven Gables";
int position = 0;
Func<string, int, int, StringComparison, int> finder =
delegate(string s, int pos, int chars, StringComparison type)
{ return title.IndexOf(s, pos, chars, type); };
do
{
int characters = title.Length - position;
position = finder("the", position, characters,
StringComparison.InvariantCultureIgnoreCase);
if (position >= 0)
{
position++;
Console.WriteLine("'The' found at position {0} in {1}.",
position, title);
}
} while (position > 0);
}
}
还可以将 lambda 表达式分配给 Func<T1,T2,TResult> 委托,如以下示例所示。 (有关 lambda 表达式的简介,请参阅 Lambda 表达式(VB)、Lambda 表达式(C#)和 Lambda 表达式(F#)。
using System;
public class DelegateExample
{
public static void Main()
{
string title = "The House of the Seven Gables";
int position = 0;
Func<string, int, int, StringComparison, int> finder =
(s, pos, chars, type) => title.IndexOf(s, pos, chars, type);
do
{
int characters = title.Length - position;
position = finder("the", position, characters,
StringComparison.InvariantCultureIgnoreCase);
if (position >= 0)
{
position++;
Console.WriteLine("'The' found at position {0} in {1}.",
position, title);
}
} while (position > 0);
}
}
open System
let title = "The House of the Seven Gables"
let finder =
Func<string, int, int, StringComparison, int>(fun s pos chars typ -> title.IndexOf(s, pos, chars, typ))
let mutable position = 0
while position > -1 do
let characters = title.Length - position
position <- finder.Invoke("the", position, characters, StringComparison.InvariantCultureIgnoreCase)
if position >= 0 then
position <- position + 1
printfn $"'The' found at position {position} in {title}."
Module DelegateExample
Public Sub Main()
Dim title As String = "The House of the Seven Gables"
Dim position As Integer = 0
Dim finder As Func(Of String, Integer, Integer, StringComparison, Integer) _
= Function(s, pos, chars, type) _
title.IndexOf(s, pos, chars, type)
Do
Dim characters As Integer = title.Length - position
position = finder("the", position, characters, _
StringComparison.InvariantCultureIgnoreCase)
If position >= 0 Then
position += 1
Console.WriteLine("'The' found at position {0} in {1}.", _
position, title)
End If
Loop While position > 0
End Sub
End Module
lambda 表达式的基础类型是泛型 Func 委托之一。 这样就可以将 lambda 表达式作为参数传递,而无需将其显式分配给委托。 具体而言,由于命名空间中的 System.Linq 许多类型方法都有 Func 参数,因此你可以将这些方法作为 lambda 表达式传递,而无需显式实例化 Func 委托。
扩展方法
| 名称 | 说明 |
|---|---|
| GetMethodInfo(Delegate) |
获取一个对象,该对象表示由指定委托表示的方法。 |
适用于
另请参阅
- Lambda 表达式 (C# 编程指南)
- Lambda 表达式:有趣的关键字 (F#)
- Lambda 表达式
- 委托(C# 编程指南)
- 委托(F#)
- Visual BasicDelegates>