gpt4 book ai didi

.net - .NET Framework 4.0中的类型差异

转载 作者:行者123 更新时间:2023-12-04 13:30:07 25 4
gpt4 key购买 nike

IEnumerable<T>IComparable<T>和其他一些现在是类型变量。 IList<T>ICollection<T>和其他很多不是。为什么?

最佳答案

.NET Framework 4.0引入了安全 co/contra-variance。 IList<T>ICollection<T>在输入和输出位置均具有T,而IEnumerable<T>仅在输出位置具有T,而IComparable<T>仅在输入位置具有T

假设IList<T>支持的类型差异:

static void FailingMethod(IList<object> list) {
list[0] = 5;
}

static void Test() {
var a = new List<string>();
a[0] = "hello";
FailingMethod(a); // if it was variant, this method call would be unsafe
}

关于.net - .NET Framework 4.0中的类型差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/932650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com