共変インターフェイスを使用すると、そのメソッドはインターフェイスで指定された型よりも多くの派生型を返すことができます。 反変インターフェイスを使用すると、そのメソッドは、インターフェイスで指定された型よりも派生型の少ないパラメーターを受け取ることができます。
.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);
}
}
こちらも参照ください
.NET