مشاركة عبر


استخدام الفرق Func و إجراء التفويضات العام (C# و Visual Basic)

توضح هذه الأمثلة كيفية إستخدام التباين المشترك و contravariance في Funcو التفويضات العامة Action تمكين إعادة إستخدام أساليب وتوفير مرونة أكبر في التعليمات البرمجية.

لمزيد من المعلومات حول التباين contravariance راجع الفرق في المفوضون (C# و Visual Basic).

إستخدام مفوضون مع معلمات نوع طردي

يوضح المثال التالي فوائد دعم التباين المشترك في عام تفويضات Func. أسلوب FindByTitle يأخذ معلمة من نوع String وتقوم بإرجاع كائن من نوع Employee. ومع ذلك، يمكنك تعيين هذه أسلوب إلى تفويض Func<String, Person> ( Func(Of String, Person)في Visual Basic) لأن 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

يوضح المثال التالي فوائد دعم Contravariant في عام تفويضات Action. أسلوبAddToContacts يأخذ معلمة من نوعPerson. ومع ذلك، يمكنك تعيين هذه أسلوب إلى تفويض Action<Employee> ( (Action(Of Employee)في Visual Basic) لأن 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;
    }
}

راجع أيضًا:

موارد أخرى

التباين المشترك و Contravariance في (C#و Visual Basic)

generics في .NET Framework