通用列表在哪里?实施重置? [英] Where does a generic List<> implement Reset?

查看:97
本文介绍了通用列表在哪里?实施重置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我转到List的定义时,我可以看到它具有实现接口IEnumerator<T>,IDisposable和IEnumerator的公共结构Enumerator.

when I go to the definition of List<> I can see it has a public struct Enumerator that implements the interfaces IEnumerator<T>, IDisposable and IEnumerator.

IEnumerator应该强制执行Reset-除了Current和MoveNext之外.但是,仅实现了Current和MoveNext. 怎么可能呢?
在哪里找到List<>的Reset()?

IEnumerator should force the implementation of Reset - besides Current and MoveNext. Yet only Current and MoveNext are implemented. How can that be?
Where do I find the Reset() of List<>?

var list = new List<int>();
list.Add(23);
list.Add(44);
var Enumerator = list.GetEnumerator();
while (Enumerator.MoveNext())
{
    Console.WriteLine(Enumerator.Current);
}
Enumerator.

当我在代码中尝试时,没有Reset():
好的-我尝试显示屏幕截图,但是他们不允许我这样做.
但是复制上面的代码后,在Enumerator的 Dot-operator(.)之后,没有显示Reset-Method.

And when I try it in code there is no Reset():
Ok - I tried to show a screenshot, but they don't let me.
But copying above code shows no Reset-Method after the Dot-operator (.) of Enumerator.

有人会知道这一点吗?

Would someone know and throw some light on this?

我看到它调用了mscorlib的一部分IEnumerator的重置.

I see it calls the Reset of IEnumerator which is part of mscorlib.

var list = new List<int>();
list.Add(23);
list.Add(44);
var Enumerator = list.GetEnumerator();
Enumerator.MoveNext();
Enumerator.MoveNext();
Console.WriteLine(Enumerator.Current);
((IEnumerator<int>)Enumerator).Reset();
Enumerator.MoveNext();

但是作为IEnumerator的接口,如何调用它呢?
IEnumerator中的Reset()应该只是一个定义,实现应留给使用该接口的任何人.
但是在某种程度上,这里仅通过定义要实现的接口即可提供实际功能.我在任何地方都看不到实际的实现,而那部分我不理解.

And yet as IEnumerator is an interface how can code be called by it?
Reset() in IEnumerator should just be a definition and the implementation left to whoever uses the interface.
But somehow here actual functionality is provided by just defining the interface to be implemented. Nowhere do I see the actual implementation - and that part I do not understand.

推荐答案

它是明确地已实现如文档中所示,与IEnumerator.Current一样.换句话说,您只能在编译时类型为IEnumerator的值上调用该方法.

It's explicitly implemented, as shown in the documentation, as is IEnumerator.Current. In other words, you can only call the method on a value with a compile-time type of IEnumerator.

因此您可以使用:

// Casing changed to be more conventional
var enumerator = list.GetEnumerator();
((IEnumerator)enumerator).Reset();

但是,无论如何,这将使该值装箱(因为List<T>.Enumerator是一个结构),这将使其毫无意义.尚不清楚您是否只是对明显缺乏Reset方法感兴趣,但总的来说,我强烈建议您依赖IEnumerator.Reset-它通常没有实现,并且IMO,它不应该是从...开始的界面的一部分.

However, that would then box the value anyway (as List<T>.Enumerator is a struct) which would make it pointless. It's not clear whether you're just interested in the apparent lack of a Reset method, but in general I would strongly advise you not to rely on IEnumerator.Reset - it's very often not implemented, and IMO it shouldn't have been part of the interface to start with...

这篇关于通用列表在哪里?实施重置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆