Func 및 Action 제네릭 대리자에 가변성 사용(C# 및 Visual Basic)

다음 예제에서는 Func 및 Action 제네릭 대리자에서 공변성(Covariance) 및 반공변성(Contravariance)을 사용하여 메서드를 다시 사용할 수 있게 하고 코드에 더 많은 융통성을 제공하는 방법을 보여 줍니다.

공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 대리자의 가변성(C# 및 Visual Basic)을 참조하십시오.

공변(covariant) 형식 매개 변수와 함께 대리자 사용

다음 예제에서는 제네릭 Func 대리자에서 지원하는 공변성(Covariance)의 이점을 보여 줍니다. FindByTitle 메서드는 String 형식의 매개 변수를 사용하고 Employee 형식의 개체를 반환합니다. 그러나 Employee가 Person을 상속하기 때문에 Func<String, Person> 대리자(Visual Basic의 경우 Func(Of String, 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 형식의 매개 변수를 사용합니다. 그러나 Employee가 Person을 상속하기 때문에 Action<Employee> 대리자(Visual Basic의 경우 (Action(Of Employee))에 이 메서드를 할당할 수 있습니다.

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의 제네릭

기타 리소스

공변성(Covariance) 및 반공변성(Contravariance)(C# 및 Visual Basic)