Kowariancja i kontrawariancja (C# i Visual Basic)

W języku C# i Visual Basic Kowariancja i kontrawariancja włączyć odwołanie niejawne konwersji typów tablicowych, typów delegatów i argumentów typu rodzajowego.Kowariancja, zachowuje zgodność przydziału i kontrawariancja odwraca go.

Poniższy kod ilustruje różnicę zgodność przypisania, Kowariancja i kontrawariancja.

' Assignment compatibility.  
Dim str As String = "test" 
' An object of a more derived type is assigned to an object of a less derived type.  
Dim obj As Object = str

' Covariance.  
Dim strings As IEnumerable(Of String) = New List(Of String)()
' An object that is instantiated with a more derived type argument  
' is assigned to an object instantiated with a less derived type argument.  
' Assignment compatibility is preserved.  
Dim objects As IEnumerable(Of Object) = strings

' Contravariance.            
' Assume that there is the following method in the class:  
' Shared Sub SetObject(ByVal o As Object) 
' End Sub 
Dim actObject As Action(Of Object) = AddressOf SetObject

' An object that is instantiated with a less derived type argument  
' is assigned to an object instantiated with a more derived type argument.  
' Assignment compatibility is reversed.  
Dim actString As Action(Of String) = actObject
// Assignment compatibility.  
string str = "test";
// An object of a more derived type is assigned to an object of a less derived type.  
object obj = str;

// Covariance. 
IEnumerable<string> strings = new List<string>();
// An object that is instantiated with a more derived type argument  
// is assigned to an object instantiated with a less derived type argument.  
// Assignment compatibility is preserved. 
IEnumerable<object> objects = strings;

// Contravariance.            
// Assume that the following method is in the class:  
// static void SetObject(object o) { } 
Action<object> actObject = SetObject;
// An object that is instantiated with a less derived type argument  
// is assigned to an object instantiated with a more derived type argument.  
// Assignment compatibility is reversed. 
Action<string> actString = actObject;

Kowariancja dla tablic umożliwia niejawna konwersja tablicy bardziej pochodny typ tablicy typu pochodnego mniej.Jednak ta operacja nie jest bezpieczne, typ, jak pokazano w poniższym przykładzie kodu.

Dim array() As Object = New String(10) {}
' The following statement produces a run-time exception. 
' array(0) = 10
object[] array = new String[10];
// The following statement produces a run-time exception. 
// array[0] = 10;

Kowariancja i kontrawariancja Obsługa grup metoda pozwala na dopasowanie podpisy metod z typów obiektów delegowanych.Dzięki temu można przypisać delegatów nie tylko te metody, które mają odpowiednie podpisów, ale także metody, które zwracają więcej pochodnych typów (kowariancja) lub że akceptować parametry, które mają mniej pochodnych typów (kontrawariancja) niż ten określony przez typ delegata.Aby uzyskać więcej informacji, zobacz Wariancje w delegatach (C# i Visual Basic) i Korzystanie z wariancji w delegatach (C# i Visual Basic).

Poniższy przykład kodu pokazuje Kowariancja i kontrawariancja obsługę dla grup metody.

Shared Function GetObject() As Object 
    Return Nothing 
End Function 

Shared Sub SetObject(ByVal obj As Object)
End Sub 

Shared Function GetString() As String 
    Return "" 
End Function 

Shared Sub SetString(ByVal str As String)

End Sub 

Shared Sub Test()
    ' Covariance. A delegate specifies a return type as object, 
    ' but you can assign a method that returns a string. 
    Dim del As Func(Of Object) = AddressOf GetString

    ' Contravariance. A delegate specifies a parameter type as string, 
    ' but you can assign a method that takes an object. 
    Dim del2 As Action(Of String) = AddressOf SetObject
End Sub
static object GetObject() { return null; }
static void SetObject(object obj) { }

static string GetString() { return ""; }
static void SetString(string str) { }

static void Test()
{
    // Covariance. A delegate specifies a return type as object, 
    // but you can assign a method that returns a string.
    Func<object> del = GetString;

    // Contravariance. A delegate specifies a parameter type as string, 
    // but you can assign a method that takes an object.
    Action<string> del2 = SetObject;
}

W programie.NET Framework 4 i Visual Studio 2010, zarówno C# i Visual Basic wsparcia Kowariancja i kontrawariancja rodzajowy interfejsów i delegatów i umożliwienia niejawna konwersja parametry typu rodzajowego.Aby uzyskać więcej informacji, zobacz Wariancje w interfejsach (C# i Visual Basic) i Wariancje w delegatach (C# i Visual Basic).

Poniższy przykład kodu pokazuje odwołanie niejawne konwersji dla rodzajowego interfejsów.

Dim strings As IEnumerable(Of String) = New List(Of String)
Dim objects As IEnumerable(Of Object) = strings
IEnumerable<String> strings = new List<String>();
IEnumerable<Object> objects = strings;

Rodzajowy interfejsem ani obiektem delegowanym jest nazywany Wariant jej parametry rodzajowe są zadeklarowana kowariantnego lub kontrawariantnego.Zarówno C# i Visual Basic umożliwiają tworzenie własnych interfejsów wariantu i delegatów.Aby uzyskać więcej informacji, zobacz Tworzenie interfejsów typu Variant (C# and Visual Basic) i Wariancje w delegatach (C# i Visual Basic).

Tematy pokrewne

Tytuł

Opis

Wariancje w interfejsach (C# i Visual Basic)

Omówiono Kowariancja i kontrawariancja w interfejsach rodzajowy i listy wariantu rodzajowy interfejsów w.NET Framework.

Tworzenie interfejsów typu Variant (C# and Visual Basic)

Pokazuje, jak utworzyć niestandardowe interfejsy wariantu.

Korzystanie z wariancji w interfejsach dla kolekcji (C# i Visual Basic)

Pokazuje, jak obsługa Kowariancja i kontrawariancja w IEnumerable i IComparable interfejsów mogą ułatwiać ponowne wykorzystywanie kodu.

Wariancje w delegatach (C# i Visual Basic)

Omówiono Kowariancja i kontrawariancja w rodzajowy i nierodzajową delegatów i listy wariantu delegatów rodzajowy w.NET Framework.

Korzystanie z wariancji w delegatach (C# i Visual Basic)

Przedstawiono sposób użycia Kowariancja i kontrawariancja wsparcia w delegatów nierodzajową odpowiadać podpisy metod z typów obiektów delegowanych.

Korzystanie z wariancji dla delegatów Func i Action (C# i Visual Basic)

Pokazuje, jak obsługa Kowariancja i kontrawariancja w Func i Action delegatów mogą ułatwiać ponowne wykorzystywanie kodu.