本文介绍了如何成员IEnumerable< T>被修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在,我被告知可以修改 IEnumerable< T> 的成员。如何做到这一点,并且当协变式类型可以被修改时出现方差违反,那么 IEnumerable T上是协变的

In an earlier question about generic variance, I was informed that members of IEnumerable<T> can be modified. How can that be done and, seeing as variance violations arise when a covariant type can be modified, how then can IEnumerable<T> be covariant on T?

推荐答案

您必须区分修改序列和修改其元素。

You have to distinguish modifying the sequence from modifying the elements of it.

您可以修改元素没有任何问题,因为您不能通过使用对象的属性和函数来修改它的类型。

You can modify elements without any problems, since you can't modify the type of the object by using properties and functions on it.

因此,这一切都很好,因为 IEnumerable< T> 中的所有元素都是 T 或源自 T 的类型。

And thus, this is all fine, since all elements in IEnumerable<T> are either T or type derived from T.

如果您能够在 IEnumerable< Derived>

这篇关于如何成员IEnumerable&lt; T&gt;被修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 01:08