实现IEnumerator(T)时出现问题 [英] problem implementing IEnumerator(T)

查看:115
本文介绍了实现IEnumerator(T)时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实现System.Collections.Generic.IEnumerator< T>时遇到问题.我必须同时实现IEnumerator< T> .Current和IEnumerator.Current.这将导致两个具有相同名称的属性发生冲突.

最有趣的事情-我无法编译 msdn示例.
它给出:

I''ve got problems implementing System.Collections.Generic.IEnumerator<T>. I have to implement both IEnumerator<T>.Current and IEnumerator.Current. This leads to a conflict of 2 properties with the same name.

The funniest thing about it - I cannot compile an msdn example.
It gives:

error CS0305: Using the generic type ''System.Collections.Generic.IEnumerator<t>'' requires ''1'' type arguments
c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll: (Related file)
error CS0738: ''Network.Node<t>.BoxEnumerator'' does not implement interface member ''System.Collections.IEnumerator.Current''. ''Network.Node<t>.BoxEnumerator.Current'' cannot implement ''System.Collections.IEnumerator.Current'' because it does not have the matching return type of ''object''.
c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll: (Related file)</t></t></t>



谁能帮我这个? C#对我来说是很新的.在此先感谢



Can anyone help me with this? C# is very new to me. Thanks in advance

推荐答案

正常执行IEnumerator<T>,并且明确地执行IEnumerator .然后让IEnumerator 版本返回IEnumerator<T>.Current.


--------

顺便说一句,要获取要编译的MSDN代码,请为Box BoxCollection添加虚拟类:

Implement IEnumerator<T> normally, and IEnumerator explicitly. Then have the IEnumerator version return the IEnumerator<T>.Current.


--------

Btw to get the MSDN code to compile, add dummy classes for Box and BoxCollection:

class Box { }

class BoxCollection : Collection<Box> { }


这里还有另一个有用的示例:
Here''s also another useful example: http://www.java2s.com/Code/CSharp/Language-Basics/ImplementgenericIEnumerableandIEnumerator.htm


这篇关于实现IEnumerator(T)时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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