在泛型集合的接口中使用变体 (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);  
    }  
}  

另请参阅