支持foreach的泛型集合类.. [英] Generics collection class that supports foreach ..

查看:75
本文介绍了支持foreach的泛型集合类..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个通用的容器类,我想支持

''foreach''语句。


我的类看起来像这样:


class MyClass< T:CollectionBase

{

// MyClass的实现< Tgoes here ...

// ...


//下一个方法导致编译器警告

IEnumerator GetEnumerator()

{

return(IEnumerator)new MyClassEnumerator< T>(this);

}


私有类MyClassEnumerator< T>:IEnumerator

{

private int idx;

private MyClass< Tobj;


// IEnumerator方法在这里实现...

}

}


警告方法是''隐藏''GetEnumerator()在

CollectionBase中 - 建议我使用new关键字。

给出了什么?我的意思是我明白了什么时候使用了新的keywod - 但是这看起来似乎太多了手持。由编译器 - 除非我在这里重新发明

轮,我自动继承枚举

我想要的功能(即使用foreach语句)

自动 - 因为我继承了

CollectionBase?

PS:我是C#的新手,但是来自几年的C / C ++背景。 br />

I am writing a generic container class which I want to support the
''foreach'' statement.

My class looks something like this:

class MyClass<T: CollectionBase
{
//Implementation for MyClass<Tgoes here ...
// ...

//The next method causes compiler warning
IEnumerator GetEnumerator()
{
return (IEnumerator) new MyClassEnumerator<T>(this) ;
}

private class MyClassEnumerator<T>: IEnumerator
{
private int idx;
private MyClass<Tobj ;

//IEnumerator methods implemented here ...
}
}

The warning is that the method ''hides'' the GetEnumerator() in
CollectionBase - and the suggestion is that I use the new keyword. What
gives? I mean I understand when the new keywod is used - but this seems
like too much "hand holding" by the compiler - Unless I am reinventing
the wheel here , and I automatically inherit the enumeration
functionality I desire (i.e. use of the foreach statement)
"automagically" - by dint of the fact that I am inheriting from
CollectionBase?
PS: I''m new to C#, but coming from several years C/C++ background.

推荐答案

CollectionBase假设您将向其内部List

属性添加项目。然后CollectionBase.GetEnumerator()遍历那个

列表,它不需要任何帮助列表。从你这样做。也许是你不应该继承的
CollectionBase。如果您想要实现列表的自定义存储和自定义枚举器,您可能希望尝试从通用接口派生(例如IList< if

a" list"充分代表你正在创作的系列。


-

Adam Clauss


比特字节 < ro ** @ your.box.com写在留言中

新闻:la ************************** **** @ bt.com ...
CollectionBase assumes you will be adding items to it''s internal List
property. The CollectionBase.GetEnumerator() then iterates across that
list, it does not need any "help" from you to do so. Maybe it is
CollectionBase that you should not be inheriting from. If you are wanting
to implement custom storage of the list, and a custom enumerator, you may
want to try deriving from the generic interfaces instead (such as IList<if
a "list" adequately represents the collection you are creating).

--
Adam Clauss

"Bit Byte" <ro**@your.box.comwrote in message
news:la******************************@bt.com...

>我正在写一个通用容器类,我想支持
''foreach' '声明。


我的班级看起来像这样:

class MyClass< T:CollectionBase

{< br $>
// MyClass的实现< Tgoes here ...

// ...


//下一个方法导致编译器警告

IEnumerator GetEnumerator()

{

return(IEnumerator)new MyClassEnumerator< T>(this);

}


私有类MyClassEnumerator< T>:IEnumerator

{

private int idx;

private MyClass< Tobj;


//这里实现的IEnumerator方法......

}

}


警告是该方法''隐藏''
CollectionBase中的GetEnumerator() - 并且我的建议是我使用new关键字。

给出了什么?我的意思是我明白了什么时候使用了新的keywod - 但是这看起来似乎太多了手持。由编译器 - 除非我在这里重新发明

轮,并且我自动继承枚举功能我希望(即使用foreach语句)自动。 - 通过

我从CollectionBase继承的事实?


PS:我是C#的新手,但来自几年C / C ++背景。
>I am writing a generic container class which I want to support the
''foreach'' statement.

My class looks something like this:

class MyClass<T: CollectionBase
{
//Implementation for MyClass<Tgoes here ...
// ...

//The next method causes compiler warning
IEnumerator GetEnumerator()
{
return (IEnumerator) new MyClassEnumerator<T>(this) ;
}

private class MyClassEnumerator<T>: IEnumerator
{
private int idx;
private MyClass<Tobj ;

//IEnumerator methods implemented here ...
}
}

The warning is that the method ''hides'' the GetEnumerator() in
CollectionBase - and the suggestion is that I use the new keyword. What
gives? I mean I understand when the new keywod is used - but this seems
like too much "hand holding" by the compiler - Unless I am reinventing the
wheel here , and I automatically inherit the enumeration functionality I
desire (i.e. use of the foreach statement) "automagically" - by dint of
the fact that I am inheriting from CollectionBase?
PS: I''m new to C#, but coming from several years C/C++ background.



Bit Byte< ro ** @ your.box.comwrote:


< ; snip>
Bit Byte <ro**@your.box.comwrote:

<snip>

警告方法是''隐藏''
CollectionBase中的GetEnumerator() - 并且建议是我使用new关键字。

给出了什么?我的意思是我明白了什么时候使用了新的keywod - 但是这看起来似乎太多了手持。由编译器 - 除非我重新发明

这里的轮子,我自动继承我想要的枚举

功能(即使用foreach语句)

自动 - 是因为我继承了

CollectionBase?
The warning is that the method ''hides'' the GetEnumerator() in
CollectionBase - and the suggestion is that I use the new keyword. What
gives? I mean I understand when the new keywod is used - but this seems
like too much "hand holding" by the compiler - Unless I am reinventing
the wheel here , and I automatically inherit the enumeration
functionality I desire (i.e. use of the foreach statement)
"automagically" - by dint of the fact that I am inheriting from
CollectionBase?



编译器做一些手持操作在这里完全合适 -

CollectionBase已经实现了IEnumerable,所以编译器是

告诉你,你隐藏了一个现有的方法。这也是

有效地通知你,你不是*覆盖现有的

方法,而是隐藏它。如果所有这些都是你想要做的,

你应该使用new - 但在这种情况下,除非你真的想要提供一个完全不同的IEnumerator语义,否则你只需要b
$ b想要编写额外的代码。请注意,如果你*想*提供不同的语义,你会得到不同的行为,具体取决于

究竟是否正在调用GetEnumerator。


基本上,因为成员隐藏不是你想经常做的事情,所以编译器希望你明确地使用new和new。修饰符当你这么做的时候,所以,如果你不是故意的话(就像这里一样)。


-

Jon Skeet - < sk *** @ pobox.com>
http: //www.pobox.com/~skeet 博客: http:// www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件

The compiler doing some hand-holding is entirely appropriate here -
CollectionBase already implement IEnumerable, so the compiler is
telling you that you''re hiding an existing method. It''s also
effectively informing you that you''re *not* overriding the existing
method, but hiding it. If all of that is really what you want to do,
you should use "new" - but in this case, unless you actually want to
provide a completely different IEnumerator semantics, you just don''t
want to write the extra code. Note that if you *do* want to provide
different semantics, you''ll get different behaviour depending on how
exactly GetEnumerator is called.

Basically, because member hiding is not something you want to do often,
the compiler wants you to explicitly use the "new" modifier when you do
so, just in case you didn''t mean to (as here).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too





比特字节 < ro ** @ your.box.com写在留言中

新闻:la ************************** **** @ bt.com ...


"Bit Byte" <ro**@your.box.comwrote in message
news:la******************************@bt.com...

>我正在写一个通用容器类,我想支持
''foreach' '声明。


我的班级看起来像这样:

class MyClass< T:CollectionBase

{< br $>
// MyClass的实现< Tgoes here ...

// ...


//下一个方法导致编译器警告

IEnumerator GetEnumerator()

{

return(IEnumerator)new MyClassEnumerator< T>(this);

}


私有类MyClassEnumerator< T>:IEnumerator

{

private int idx;

private MyClass< Tobj;


//这里实现的IEnumerator方法......

}

}
>I am writing a generic container class which I want to support the
''foreach'' statement.

My class looks something like this:

class MyClass<T: CollectionBase
{
//Implementation for MyClass<Tgoes here ...
// ...

//The next method causes compiler warning
IEnumerator GetEnumerator()
{
return (IEnumerator) new MyClassEnumerator<T>(this) ;
}

private class MyClassEnumerator<T>: IEnumerator
{
private int idx;
private MyClass<Tobj ;

//IEnumerator methods implemented here ...
}
}



你不需要实现GetEnumerator,基类就是这样做的。如果你想要添加一个类型化的枚举器声明你实现了IEnumerable< Tand

那么明确的实现。


像这样:


类MyClass< T:CollectionBase,IEnumerable< T>

{


私有类MyClassEnumerator:IEnumerator< T>

{

private int idx;

private MyClass< Tobj;


public MyClassEnumerator (MyClass< TmyClass)

{

obj = MyClass;

}


// IEnumerator这里实现的方法......

}


IEnumerator< TIEnumerable< T> .GetEnumerator()

{

返回新的MyClassEnumerator< T>(this);

}


}


David

You don''t need to implement GetEnumerator, the base class does that. If you
want to add a typed enumerator declare that you implement IEnumerable<Tand
implement that explicily.

Like this:

class MyClass<T: CollectionBase, IEnumerable<T>
{

private class MyClassEnumerator : IEnumerator<T>
{
private int idx;
private MyClass<Tobj;

public MyClassEnumerator(MyClass<TmyClass)
{
obj = MyClass;
}

//IEnumerator methods implemented here ...
}

IEnumerator<TIEnumerable<T>.GetEnumerator()
{
return new MyClassEnumerator<T>(this);
}

}

David


这篇关于支持foreach的泛型集合类..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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