简体   繁体   English

为什么.Concat方法的返回值类型看起来有点奇怪:System.Linq.Enumerable +(...)?

[英]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.

相关问题 Linq Excel文件打印SYSTEM.LINQ.ENUMERABLE +而不是值C# - Linq Excel file prints SYSTEM.LINQ.ENUMERABLE+ instead of value C# 方法系统linq的类型参数可枚举为可枚举-错误 - the type arguments for method system linq enumerable as enumerable - Error 为什么System.Linq.Enumerable中没有SelectToArray方法 - Why there is no SelectToArray method in System.Linq.Enumerable 类型“ System.Linq.Enumerable”上不存在方法“ SelectToken” - No method 'SelectToken' exists on type 'System.Linq.Enumerable' 类型&#39;System.Linq.Enumerable&#39;上的通用方法&#39;Sum&#39;不兼容 - No generic method 'Sum' on type 'System.Linq.Enumerable' is compatible LINQ Enumerable中的Enumerable-为什么? - LINQ Enumerable in Enumerable - Why? 方法&#39;System.linq.Enumerable.singleOrDefault的类型参数 <TSource> &#39;无法从用法中推断 - the type arguments for method 'System.linq.Enumerable.singleOrDefault<TSource>' cannot be inferred from the usage 无法从用法中推断方法 System.Linq.Enumerable.OrderBy 的类型参数 - The type arguments for method System.Linq.Enumerable.OrderBy cannot be inferred from the usage 传递到字典中的模型项的类型为&#39;System.Linq.Enumerable - The model item passed into the dictionary is of type 'System.Linq.Enumerable 为什么Lambda表达式会返回System.Linq.Enumerable + WhereSelectEnumerableIterator`2 - Why Lambda expression returns System.Linq.Enumerable+WhereSelectEnumerableIterator`2 as a result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM