B
Bit Byte
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<T> goes 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<T> obj ;
//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.
'foreach' statement.
My class looks something like this:
class MyClass<T> : CollectionBase
{
//Implementation for MyClass<T> goes 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<T> obj ;
//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.