다음을 통해 공유


제네릭 컬렉션에 대한 인터페이스의 가변성 사용(C#)

공변 인터페이스를 사용하면 해당 메서드가 인터페이스에 지정된 형식보다 더 많은 파생 형식을 반환할 수 있습니다. 반공변 인터페이스를 사용하면 해당 메서드가 인터페이스에 지정된 것보다 덜 파생된 형식의 매개 변수를 수락할 수 있습니다.

.NET Framework 4에서는 여러 기존 인터페이스가 공변성 및 반공변성이 되었습니다. 여기에는 IEnumerable<T>IComparable<T>이 포함됩니다. 이렇게 하면 파생 형식의 컬렉션에 대한 기본 형식의 제네릭 컬렉션과 함께 작동하는 메서드를 다시 사용할 수 있습니다.

.NET의 변형 인터페이스 목록은 제네릭 인터페이스의 분산(C#)을 참조하세요.

제네릭 컬렉션 변환

다음 예제에서는 인터페이스에서 공변성 지원의 이점을 보여 줍니다 IEnumerable<T> . 메서드는 PrintFullName 형식의 IEnumerable<Person> 컬렉션을 매개 변수로 허용합니다. 그러나 IEnumerable<Employee>Employee를 상속하기 때문에 Person 형식의 컬렉션에 다시 사용할 수 있습니다.

// Simple hierarchy of classes.  
public class Person  
{  
    public string FirstName { get; set; }  
    public string LastName { get; set; }  
}  
  
public class Employee : Person { }  
  
class Program  
{  
    // The method has a parameter of the IEnumerable<Person> type.  
    public static void PrintFullName(IEnumerable<Person> persons)  
    {  
        foreach (Person person in persons)  
        {  
            Console.WriteLine("Name: {0} {1}",  
            person.FirstName, person.LastName);  
        }  
    }  
  
    public static void Test()  
    {  
        IEnumerable<Employee> employees = new List<Employee>();  
  
        // You can pass IEnumerable<Employee>,
        // although the method expects IEnumerable<Person>.  
  
        PrintFullName(employees);  
  
    }  
}  

제네릭 컬렉션 비교

다음 예제는 IEqualityComparer<T> 인터페이스의 반공변성 지원이 어떻게 이점을 제공하는지를 보여 줍니다. PersonComparer 클래스가 IEqualityComparer<Person> 인터페이스를 구현합니다. 하지만 Employee이(가) Employee을(를) 상속하므로 이 클래스를 다시 사용하여 Person 형식의 객체 시퀀스를 비교할 수 있습니다.

// Simple hierarchy of classes.  
public class Person  
{  
    public string FirstName { get; set; }  
    public string LastName { get; set; }  
}  
  
public class Employee : Person { }  
  
// The custom comparer for the Person type  
// with standard implementations of Equals()  
// and GetHashCode() methods.  
class PersonComparer : IEqualityComparer<Person>  
{  
    public bool Equals(Person x, Person y)  
    {
        if (Object.ReferenceEquals(x, y)) return true;  
        if (Object.ReferenceEquals(x, null) ||  
            Object.ReferenceEquals(y, null))  
            return false;
        return x.FirstName == y.FirstName && x.LastName == y.LastName;  
    }  
    public int GetHashCode(Person person)  
    {  
        if (Object.ReferenceEquals(person, null)) return 0;  
        int hashFirstName = person.FirstName == null  
            ? 0 : person.FirstName.GetHashCode();  
        int hashLastName = person.LastName.GetHashCode();  
        return hashFirstName ^ hashLastName;  
    }  
}  
  
class Program  
{  
  
    public static void Test()  
    {  
        List<Employee> employees = new List<Employee> {  
               new Employee() {FirstName = "Michael", LastName = "Alexander"},  
               new Employee() {FirstName = "Jeff", LastName = "Price"}  
            };  
  
        // You can pass PersonComparer,
        // which implements IEqualityComparer<Person>,  
        // although the method expects IEqualityComparer<Employee>.  
  
        IEnumerable<Employee> noduplicates =  
            employees.Distinct<Employee>(new PersonComparer());  
  
        foreach (var employee in noduplicates)  
            Console.WriteLine(employee.FirstName + " " + employee.LastName);  
    }  
}  

참고하십시오