Func<T,TResult> Delegát
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Zapouzdřuje metodu, která má jeden parametr, a vrátí hodnotu typu určeného parametrem TResult .
generic <typename T, typename TResult>
public delegate TResult Func(T arg);
public delegate TResult Func<in T,out TResult>(T arg);
public delegate TResult Func<in T,out TResult>(T arg) where T : allows ref struct where TResult : allows ref struct;
public delegate TResult Func<T,TResult>(T arg);
type Func<'T, 'Result> = delegate of 'T -> 'Result
Public Delegate Function Func(Of In T, Out TResult)(arg As T) As TResult
Public Delegate Function Func(Of T, TResult)(arg As T) As TResult
Parametry typu
- T
Typ parametru metody, kterou tento delegát zapouzdřuje.
Tento parametr typu je kontravariantní. To znamená, že můžete použít buď zadaný typ, nebo libovolný typ, který je méně odvozený. Další informace o kovarianci a kontravarianci najdete v tématu popisujícím kovarianci a kontravarianci u parametrického polymorfismu.- TResult
Typ návratové hodnoty metody, kterou tento delegát zapouzdřuje.
Tento parametr typu je kovariantní. To znamená, že můžete použít buď zadaný typ, nebo libovolný typ, který je více odvozený. Další informace o kovarianci a kontravarianci najdete v tématu popisujícím kovarianci a kontravarianci u parametrického polymorfismu.Parametry
- arg
- T
Parametr metody, kterou tento delegát zapouzdřuje.
Návratová hodnota
Návratová hodnota metody, kterou tento delegát zapouzdřuje.
Příklady
Následující příklad ukazuje, jak deklarovat a používat delegáta Func<T,TResult> . Tento příklad deklaruje proměnnou Func<T,TResult> a přiřadí jí výraz lambda, který převede znaky v řetězci na velká písmena. Delegát, který zapouzdřuje tuto metodu, se následně předá Enumerable.Select metodě, aby změnil řetězce v poli řetězců na velká písmena.
// 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
*/
open System
open System.Linq
// Declare a Func variable and assign a lambda expression to the
// variable. The function takes a string and converts it to uppercase.
let selector = Func<string, string>(fun str -> str.ToUpper())
// Create a list of strings.
let words = [ "orange"; "apple"; "Article"; "elephant" ]
// Query the list and select strings according to the selector function.
let aWords = words.Select selector
// Output the results to the console.
for word in aWords do
printfn $"{word}"
// This code example produces the following output:
// ORANGE
// APPLE
// ARTICLE
// ELEPHANT
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
Poznámky
Tento delegát můžete použít k reprezentaci metody, která se dá předat jako parametr bez explicitního deklarování vlastního delegáta. Zapouzdřená metoda musí odpovídat podpisu metody definovanému tímto delegátem. To znamená, že zapouzdřená metoda musí mít jeden parametr předaný hodnotou a že musí vrátit hodnotu.
Note
Chcete-li odkazovat na metodu, která má jeden parametr a vrací void (nebo v Visual Basic, která je deklarována jako Sub místo jako Function), použijte místo toho obecný delegát Action<T>.
Při použití Func<T,TResult> delegáta nemusíte explicitně definovat delegáta, který zapouzdřuje metodu s jedním parametrem. Například následující kód explicitně deklaruje delegát pojmenovaný ConvertMethod a přiřadí odkaz na metodu UppercaseString jeho instanci delegáta.
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();
}
}
type ConvertMethod = delegate of string -> string
let uppercaseString (inputString: string) =
inputString.ToUpper()
// Instantiate delegate to reference uppercaseString function
let convertMeth = ConvertMethod uppercaseString
let name = "Dakota"
// Use delegate instance to call uppercaseString function
printfn $"{convertMeth.Invoke name}"
' 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
Následující příklad tento kód zjednodušuje vytvořením instance delegáta Func<T,TResult> místo explicitního definování nového delegáta a přiřazení pojmenované metody k němu.
// 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));
string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
// This code example produces the following output:
//
// DAKOTA
open System
let uppercaseString (inputString: string) =
inputString.ToUpper()
// Instantiate delegate to reference uppercaseString function
let convertMethod = Func<string, string> uppercaseString
let name = "Dakota"
// Use delegate instance to call uppercaseString function
printfn $"{convertMethod.Invoke name}"
// This code example produces the following output:
// DAKOTA
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
Delegáta můžete také použít Func<T,TResult> s anonymními metodami v jazyce C#, jak je znázorněno v následujícím příkladu. (Úvod do anonymních metod naleznete v tématu Anonymní metody.)
Func<string, string> convert = delegate(string s)
{ return s.ToUpper();};
string name = "Dakota";
Console.WriteLine(convert(name));
// This code example produces the following output:
//
// DAKOTA
Výraz lambda můžete také přiřadit delegátovi Func<T,TResult> , jak ukazuje následující příklad. (Úvod do výrazů lambda najdete v tématu Výrazy lambda (VB),výrazy lambda (C#) a výrazy lambda (F#).)
Func<string, string> convert = s => s.ToUpper();
string name = "Dakota";
Console.WriteLine(convert(name));
// This code example produces the following output:
//
// DAKOTA
open System
let convert = Func<string, string>(fun s -> s.ToUpper())
let name = "Dakota"
printfn $"{convert.Invoke name}"
// This code example produces the following output:
// DAKOTA
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
Základní typ výrazu lambda je jedním z obecných Func delegátů. Díky tomu je možné předat výraz lambda jako parametr, aniž byste ho explicitně přiřadili delegátovi. Zejména proto, že mnoho metod typů v System.Linq oboru názvů má Func<T,TResult> parametry, můžete tyto metody předat výraz lambda, aniž byste explicitně vytvořili instanci delegáta Func<T,TResult> .
Metody rozšíření
| Name | Description |
|---|---|
| GetMethodInfo(Delegate) |
Získá objekt, který představuje metodu reprezentovanou zadaným delegátem. |