协方差就是接受值吗?
Is covariance all about accepting values?
协方差就是接受值吗?
我正在通过 CLR via C# 书学习 C#。我看到了以下摘录:
Since T is covariant, it is possible to have the following code compile and run successfully:
// This method accepts an IEnumerable of any reference type
Int32 Count(IEnumerable<Object> collection) { ... }
...
// The call below passes an IEnumerable<String> to Count
Int32 c = Count(new[] { "Grant" });
我在这里很困惑。因为协方差是关于具有作为所需类型的基本类型之一的类型。因此,协方差仅在 return 类型的上下文中使用。虽然在上面的示例中我们有一个 String
(从 Object
派生,因此它是逆变的,但不是协变的)用于传递参数的上下文(但不是 return ing 值)。
那么,上面的例子中我们是不是应该用逆变而不是协变呢(也就是说书上有错误)?
更新
评论后我又收到了一个问题。以下定义正确吗?
Contravariant Meaning that the generic type parameter can change from a class to a class
derived from it. In C#, you indicate contravariant generic type parameters with the in keyword.
Contravariant generic type parameters can appear only in input positions such as a method’s
argument.
Covariant Meaning that the generic type argument can change from a class to one of its base
classes. In C#, you indicate covariant generic type parameters with the out keyword. Covariant
正如乔希指出的那样,这本书是正确的。
如果您想从其他来源确认,可以检查this link。
IEnumerable<Cat>
is a subtype of IEnumerable<Animal>
. The subtyping is preserved because IEnumerable<T>
is covariant on T
.
C# 中针对这些概念的两个关键字是 out
协变和 in
逆变。在 IEnumerable<T>
的情况下,这转换为 IEnumerable<out T>
。
希望这对您有所帮助。
更新
您必须按如下方式反转您的定义。
Covariant Meaning that the generic type parameter can be a certain class and all the derived classes from it (IEnumerable<Object>
can be a IEnumerable<String>
since String
is a subtype of Object
). In C#, you indicate covariant generic type parameters with the out keyword.
Contravariant Meaning that the generic type argument can change from a
class to one of its base classes. In C#, you indicate contravariant
generic type parameters with the in keyword.
协方差就是接受值吗?
我正在通过 CLR via C# 书学习 C#。我看到了以下摘录:
Since T is covariant, it is possible to have the following code compile and run successfully:
// This method accepts an IEnumerable of any reference type
Int32 Count(IEnumerable<Object> collection) { ... }
...
// The call below passes an IEnumerable<String> to Count
Int32 c = Count(new[] { "Grant" });
我在这里很困惑。因为协方差是关于具有作为所需类型的基本类型之一的类型。因此,协方差仅在 return 类型的上下文中使用。虽然在上面的示例中我们有一个 String
(从 Object
派生,因此它是逆变的,但不是协变的)用于传递参数的上下文(但不是 return ing 值)。
那么,上面的例子中我们是不是应该用逆变而不是协变呢(也就是说书上有错误)?
更新
评论后我又收到了一个问题。以下定义正确吗?
Contravariant Meaning that the generic type parameter can change from a class to a class derived from it. In C#, you indicate contravariant generic type parameters with the in keyword. Contravariant generic type parameters can appear only in input positions such as a method’s argument.
Covariant Meaning that the generic type argument can change from a class to one of its base classes. In C#, you indicate covariant generic type parameters with the out keyword. Covariant
正如乔希指出的那样,这本书是正确的。
如果您想从其他来源确认,可以检查this link。
IEnumerable<Cat>
is a subtype ofIEnumerable<Animal>
. The subtyping is preserved becauseIEnumerable<T>
is covariant onT
.
C# 中针对这些概念的两个关键字是 out
协变和 in
逆变。在 IEnumerable<T>
的情况下,这转换为 IEnumerable<out T>
。
希望这对您有所帮助。
更新
您必须按如下方式反转您的定义。
Covariant Meaning that the generic type parameter can be a certain class and all the derived classes from it (
IEnumerable<Object>
can be aIEnumerable<String>
sinceString
is a subtype ofObject
). In C#, you indicate covariant generic type parameters with the out keyword.Contravariant Meaning that the generic type argument can change from a class to one of its base classes. In C#, you indicate contravariant generic type parameters with the in keyword.