共用方式為


針對 Func 與 Action 委派使用變異數 (C# 和 Visual Basic)

以下範例會示範如何在 Func 和 Action 泛型委派中使用共變數與 Contravariance,讓方法可以重複使用並提供程式碼更多彈性。

如需共變數與 Contravariance 的詳細資訊,請參閱委派中的變異數 (C# 和 Visual Basic)

使用具有 Covariant 型別參數的委派

下列範例說明在 Func 泛型委派中支援共變數的好處。 FindByTitle 方法接受型別為 String 的參數,而且會傳回型別為 Employee 的物件。 但是您可以將此方法指派給 Func<String, Person> (在 Visual Basic 中則為 Func(Of String, Person)) 委派,因為 Employee 會繼承 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;

    }
}

使用具有 Contravariant 型別參數的委派

下列範例說明在 Action 泛型委派中支援 Contravariance 的好處。 AddToContacts 方法接受型別為 Person 的參數。 但是您可以將此方法指派給 Action<Employee> (在 Visual Basic 中則為 (Action(Of Employee)) 委派,因為 Employee 會繼承 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;
    }
}

請參閱

概念

.NET Framework 中的泛型

其他資源

共變數和反變數 (C# 和 Visual Basic)