什么样的类就能够产生收益回报 [英] What kind of class does yield return return
问题描述
所以,我已经注意到,此代码如下:
类节目
{
公共静态无效的主要()
{
的Int32 []号= {1,2,3,4,5};
使用(VAR枚举=数据()的GetEnumerator())
{
}
}
公共静态IEnumerable的<串GT;数据()
{
收益率的回报东西;
}
}
在特别的,我很好奇的使用
块,因为:
的Int32 []号= {1,2, 3,4,5,6};使用
(VAR枚举= numbers.GetEnumerator())
{
}
失败,编译错误。显然,收益回报率
返回类是的IDisposable
而普通阵列枚举是没有的。所以现在我很好奇:究竟是什么收益回报率
创建
的IEnumerator< T>
工具的IDisposable
,你可以在对象浏览器或MSDN见 非普通的IEnumerator
没有。
该基地阵列
类的IEnumerable
而不是的IEnumerable< T>
。 (因为阵列
不是一般的)结果
混凝土数组类型不落实的IEnumerable< T>
,但他们实施的GetEnumerator()
明确的(我不知道为什么)。结果
因此,的GetEnumerator()
对任何数组类型的回报可见的IEnumerator
。
通用的IEnumerable< ; T>
实施返回 System.SZArrayHelper.SZGenericArrayEnumerator< T>
此类的源代码(在 Array.cs
)有如下评论这部分解释了这个(记住,通用阵列都支持可以追溯到的时候,的IEnumerable< T>
不是contraviant)
// ------ -------------------------------------------------- -------------------------------
//!阅读此之前你工作这个类。
//
//这个类中的方法必须非常小心地写到避免引入安全漏洞。
//这是因为他们被调用的特殊本! 此对象
//所有这些方法都不是SZArrayHelper对象。相反,它们是U型[]
//其中U []是强制转换为T []的。没有实际SZArrayHelper对象已经被实例化。因此,你将
//看到了很多投这个的T []的表情。
//
//需要这个类,允许类的SZ数组T []揭露的IList< T> ;,
//的IList< T.BaseType>等,等,一路攀升到IList<对象> ;.当下面的调用是
//做:
//
//((IList的< T>)(新U [N]))SomeIListMethod()
//
//接口存根调度员将此视为一个特殊的情况下,负载高达SZArrayHelper,
//找到相应的通用法(方法的名称简单匹配),实例化
//它的类型< T>并执行。
//
//的T将反映用来调用该方法的接口。实际运行时间这个将是
//数组,它是可浇注为T [](即对primitivs和值类型,这将是准确
//T [] - 为orefs,它可能是一个U []其中U从T.派生)
// ---------------------------- -------------------------------------------------- ---------
So I've notice that this code works:
class Program
{
public static void Main()
{
Int32[ ]numbers = {1,2,3,4,5};
using (var enumerator = Data().GetEnumerator())
{
}
}
public static IEnumerable<String> Data()
{
yield return "Something";
}
}
In particular, I'm curious about the using
block, since:
Int32[] numbers = { 1, 2, 3, 4, 5, 6 };
using (var enumerator = numbers.GetEnumerator())
{
}
fails with a compiler error. Obviously, the class that yield return
returns is IDisposable
while a regular array enumerator is not. So now I'm curious: what exactly does yield return
create?
IEnumerator<T>
implements IDisposable
, as you can see in the Object Browser or in MSDN.
The non-generic IEnumerator
does not.
The base Array
class implements IEnumerable
but not IEnumerable<T>
. (since Array
is not generic)
Concrete array types do implement IEnumerable<T>
, but they implement GetEnumerator()
explicitly (I'm not sure why).
Therefore, the GetEnumerator()
visible on any array type returns IEnumerator
.
The generic IEnumerable<T>
implementation returns a System.SZArrayHelper.SZGenericArrayEnumerator<T>
.
The source code for this class (in Array.cs
) has the following comment which partially explains this (remember, all support for generic arrays dates back to a time when IEnumerable<T>
was not contraviant)
//---------------------------------------------------------------------------------------
// ! READ THIS BEFORE YOU WORK ON THIS CLASS.
//
// The methods on this class must be written VERY carefully to avoid introducing security holes.
// That's because they are invoked with special "this"! The "this" object
// for all of these methods are not SZArrayHelper objects. Rather, they are of type U[]
// where U[] is castable to T[]. No actual SZArrayHelper object is ever instantiated. Thus, you will
// see a lot of expressions that cast "this" "T[]".
//
// This class is needed to allow an SZ array of type T[] to expose IList<T>,
// IList<T.BaseType>, etc., etc. all the way up to IList<Object>. When the following call is
// made:
//
// ((IList<T>) (new U[n])).SomeIListMethod()
//
// the interface stub dispatcher treats this as a special case, loads up SZArrayHelper,
// finds the corresponding generic method (matched simply by method name), instantiates
// it for type <T> and executes it.
//
// The "T" will reflect the interface used to invoke the method. The actual runtime "this" will be
// array that is castable to "T[]" (i.e. for primitivs and valuetypes, it will be exactly
// "T[]" - for orefs, it may be a "U[]" where U derives from T.)
//---------------------------------------------------------------------------------------
这篇关于什么样的类就能够产生收益回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!