Utilisation de la variance pour les délégués génériques Func et Action (C# et Visual Basic)
Ces exemples montrent comment utiliser la covariance et la contravariance dans les délégués génériques Func et Action pour activer la réutilisation des méthodes et fournir davantage de flexibilité dans votre code.
Pour plus d'informations sur la covariance et la contravariance, consultez Variance dans les délégués (C# et Visual Basic).
Utilisation des délégués avec les paramètres de type covariant
L'exemple suivant illustre les avantages de la prise en charge de la covariance dans les délégués génériques Func. La méthode FindByTitle prend un paramètre de type String et retourne un objet de type Employee. Toutefois, vous pouvez assigner cette méthode au délégué Func<String, Person> (Func(Of String, Person) en Visual Basic) car Employee hérite de Person.
' Simple hierarchy of classes.
Public Class Person
End Class
Public Class Employee
Inherits Person
End Class
Class Finder
Public Shared Function FindByTitle(
ByVal title As String) As Employee
' This is a stub for a method that returns
' an employee that has the specified title.
Return New Employee
End Function
Sub Test()
' Create an instance of the delegate without using variance.
Dim findEmployee As Func(Of String, Employee) =
AddressOf FindByTitle
' The delegate expects a method to return Person,
' but you can assign it a method that returns Employee.
Dim findPerson As Func(Of String, Person) =
AddressOf FindByTitle
' You can also assign a delegate
' that returns a more derived type to a delegate
' that returns a less derived type.
findPerson = findEmployee
End Sub
End Class
// Simple hierarchy of classes.
public class Person { }
public class Employee : Person { }
class Program
{
static Employee FindByTitle(String title)
{
// This is a stub for a method that returns
// an employee that has the specified title.
return new Employee();
}
static void Test()
{
// Create an instance of the delegate without using variance.
Func<String, Employee> findEmployee = FindByTitle;
// The delegate expects a method to return Person,
// but you can assign it a method that returns Employee.
Func<String, Person> findPerson = FindByTitle;
// You can also assign a delegate
// that returns a more derived type
// to a delegate that returns a less derived type.
findPerson = findEmployee;
}
}
Utilisation des délégués avec les paramètres de type contravariant
L'exemple suivant illustre les avantages de la prise en charge de la contravariance dans les délégués génériques Action. La méthode AddToContacts prend un paramètre de type Person. Toutefois, vous pouvez assigner cette méthode au délégué Action<Employee> ((Action(Of Employee) en Visual Basic) car Employee hérite de Person.
Public Class Person
End Class
Public Class Employee
Inherits Person
End Class
Class AddressBook
Shared Sub AddToContacts(ByVal person As Person)
' This method adds a Person object
' to a contact list.
End Sub
Sub Test()
' Create an instance of the delegate without using variance.
Dim addPersonToContacts As Action(Of Person) =
AddressOf AddToContacts
' The Action delegate expects
' a method that has an Employee parameter,
' but you can assign it a method that has a Person parameter
' because Employee derives from Person.
Dim addEmployeeToContacts As Action(Of Employee) =
AddressOf AddToContacts
' You can also assign a delegate
' that accepts a less derived parameter
' to a delegate that accepts a more derived parameter.
addEmployeeToContacts = addPersonToContacts
End Sub
End Class
public class Person { }
public class Employee : Person { }
class Program
{
static void AddToContacts(Person person)
{
// This method adds a Person object
// to a contact list.
}
static void Test()
{
// Create an instance of the delegate without using variance.
Action<Person> addPersonToContacts = AddToContacts;
// The Action delegate expects
// a method that has an Employee parameter,
// but you can assign it a method that has a Person parameter
// because Employee derives from Person.
Action<Employee> addEmployeeToContacts = AddToContacts;
// You can also assign a delegate
// that accepts a less derived parameter to a delegate
// that accepts a more derived parameter.
addEmployeeToContacts = addPersonToContacts;
}
}
Voir aussi
Concepts
Génériques dans le .NET Framework