[英]Why .Concat method's return value type looks a bit weird: System.Linq.Enumerable+(…)?
I have 2 instances: 我有2个实例:
foo and bar
Their types are: 他们的类型是:
foo.GetType().ToString()
returns: System.Collections.Generic.List`1[MyNameSpace.MyClass] 返回: System.Collections.Generic.List`1 [MyNameSpace.MyClass]
bar.GetType().ToString()
returns: System.Collections.Generic.List`1[MyNameSpace.MyClass] 返回: System.Collections.Generic.List`1 [MyNameSpace.MyClass]
When I concatanate them: 当我将它们连接起来时:
var foobar = foo.Concat(bar);
The GetType() returns System.Linq.Enumerable+d__71`1[MyNameSpace.MyClass] GetType()返回System.Linq.Enumerable + d__71`1 [MyNameSpace.MyClass]
Question : What does this mean? 问题 :这是什么意思? Should not it be IEnumerable ?
不应该是IEnumerable吗?
Don't confuse the declared return type and the actual type of the returned value. 不要混淆声明的返回类型和返回值的实际类型。
Concat
is declared to return IEnumerable<T>
, but it actually returns an instance of a concrete type that implements IEnumerable<T>
. 声明
Concat
返回IEnumerable<T>
,但它实际上返回一个实现IEnumerable<T>
的具体类型的实例。 GetType
returns the concrete type, not the declared return type. GetType
返回具体类型,而不是声明的返回类型。
As for the weird name, Concat
is implemented with an iterator block , and iterator blocks are transformed by the compiler into types with names such as Enumerable+d__71
that implement IEnumerable<T>
. 至于奇怪的名称,
Concat
是用迭代器块实现的, 迭代器块由编译器转换为名称为Enumerable+d__71
,实现IEnumerable<T>
。
In addition to Thomas' answer, note that the "`1" portion of the name indicates the number of generic arguments. 除了托马斯的回答,请注意名称的“1”部分表示通用参数的数量。 The type for each generic argument follows in the brackets "[...]".
每个泛型参数的类型在括号“[...]”后面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.