IEnumerable的<>不行 [英] IEnumerable<> won't work

查看:94
本文介绍了IEnumerable的<>不行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用VS 2005.我有一个''IpForm''类和一个''IpFormCollection''类,

包含IpForm对象。要使用

foreach迭代IpFrom对象,该类实现如下:

公共类IpFormCollection:IEnumerable< IpForm>

{

ArrayList forms = new ArrayList();


public IEnumerator< IpFormGetEnumerator()

{

foreach(IpForm f in this.forms)

{

收益率返回f;

}

}


...

}


从我在这里学到的东西

http://www.ondotnet。 com / pub / a / dotnet ... 7 / liberty.html


这应该有用,但是VS说这个类没有实现
interface member''System.Collections.IEnumerable.GetEnumerator()''。"并且

GetEnumerator()''是静态的,不是公开的,或者有错误的

返回类型。


Gustaf

Using VS 2005. I got an ''IpForm'' class and an ''IpFormCollection'' class,
containing IpForm objects. To iterate through IpFrom objects with
foreach, the class is implemented as such:

public class IpFormCollection : IEnumerable<IpForm>
{
ArrayList forms = new ArrayList();

public IEnumerator<IpFormGetEnumerator()
{
foreach (IpForm f in this.forms)
{
yield return f;
}
}

...
}

From what I learned here

http://www.ondotnet.com/pub/a/dotnet...7/liberty.html

this ought to work, but VS says that the class "does not implement
interface member ''System.Collections.IEnumerable.GetEnumerator()''." and
that "GetEnumerator()'' is either static, not public, or has the wrong
return type."

Gustaf

推荐答案

IEnumerable< Tis本身派生自IEnumerable;这里通常的解决方案

是创建一个显式实现,并转发请求:


IEnumerator IEnumerable.GetEnumerator(){

返回GetEnumerator(); //返回公共,键入一个

}


此外,如果这是2.0,我会将表单设为List< IpForm> ;;然后这是强类型的
,并且具有可以放弃枚举器的副作用:

只返回this.forms.GetEnumerator()而不是foreach / yield返回


看看是否有帮助...


Marc
IEnumerable<Tis itself derived from IEnumerable; the usual solution here
is to create an explicit implementation, and forward the request:

IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator(); // returns the public, typed one
}

Also, if this is 2.0, I would make forms a List<IpForm>; this is then
strongly typed, and has the side-advantage that you can drop an enumerator:
simply return this.forms.GetEnumerator() instead of the foreach/yield return

See if that helps...

Marc


Marc Gravell写道:
Marc Gravell wrote:

IEnumerable< Tis本身派生自IEnumerable;这里通常的解决方案

是创建一个显式实现,并转发请求:


IEnumerator IEnumerable.GetEnumerator(){

返回GetEnumerator(); //返回公共,键入一个

}


此外,如果这是2.0,我会将表单设为List< IpForm> ;;然后这是强类型的
,并且具有可以放弃枚举器的副作用:

只返回this.forms.GetEnumerator()而不是foreach / yield返回
IEnumerable<Tis itself derived from IEnumerable; the usual solution here
is to create an explicit implementation, and forward the request:

IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator(); // returns the public, typed one
}

Also, if this is 2.0, I would make forms a List<IpForm>; this is then
strongly typed, and has the side-advantage that you can drop an enumerator:
simply return this.forms.GetEnumerator() instead of the foreach/yield return



谢谢。你的意思是替换GetEnumerator()代码吗?


公共类IpFormCollection:IEnumerable< IpForm>

{

列表< IpFormforms =新列表< IpForm>();


IEnumerator IEnumerable.GetEnumerator()

{

返回GetEnumerator( );

}


我现在又收到了另一个错误。它说集合类没有

实现接口成员

''System.Collections.Generic.IEnumerable< MyNamespac e.IpForm> .GetEnumerator()''" ;。


Gustaf

Thank you. Do you mean replacing the GetEnumerator() code, like this?

public class IpFormCollection : IEnumerable<IpForm>
{
List<IpFormforms = new List<IpForm>();

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

I''m getting another error now. It says the collection class "does not
implement interface member
''System.Collections.Generic.IEnumerable<MyNamespac e.IpForm>.GetEnumerator()''".

Gustaf


我有一个相关的问题。当使用公共IEnumerator GetEnumerator()方法而不实际使用类中的接口实现接口时,类会获得foreach迭代功能。为什么这样,为什么不需要

接口实现?同样的问题适用于IClonable

。请详细解释一下。
I have a related question. A class gets a foreach iteration capability when
public IEnumerator GetEnumerator() method is used without actually
implementing the interface in the class. Why is it so and why isnt the
interface implementation necessary? Same question applies to IClonable as
well. Kindly explain in a little detail.


这篇关于IEnumerable的&LT;&GT;不行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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