T
Tony Johansson
Hello!!
Based on the book I read and on my previous question and answers from you it
seems to me that
it's really very rare that I have to implement IEnumerable(one method) and
IEnumerator(three methods) because these are already implemented I think in
every collection class in .Net so I can just use them if I'm not use the
foreach construction instead.
My first question:
So when can it be useful to be obliged to have to Implement these two
interfaces?
Assume for some reason that I must implement these two interfaces.
One method in the IEnumerator namely Current is not type safe because it
return an object
Assume also that I want to have type safe methods so I use generic interface
for IEnumerator<T>
My second question:
Because of method Current is not type safe it seems to be that it's enough
to
use the generic interface for IEnumerator<T> ? which mean I don't have to
use the
generic interface IEnumerable<T> but instead the the IEnumerable
Now to my third question below is an attempt to implement the two interfaces
IEnumerator<T> and IEnumerable in classSomeClass below.
Can you correct me because I will most certain have several errors ?
In this class I must implement four methods these are
MoveNext, Reset and Current for interface IEnumerator<T> and
one method GetEnumerator for interface IEnumerable
class SomeClass<T> : IEnumerable, where T : IEnumerator<T>
{
public bool MoveNext()
{...}
public void Reset()
{...}
public T Current()
{ return T; }
public IEnumerator GetEnumerator()
{ ...}
}
//Tony
Based on the book I read and on my previous question and answers from you it
seems to me that
it's really very rare that I have to implement IEnumerable(one method) and
IEnumerator(three methods) because these are already implemented I think in
every collection class in .Net so I can just use them if I'm not use the
foreach construction instead.
My first question:
So when can it be useful to be obliged to have to Implement these two
interfaces?
Assume for some reason that I must implement these two interfaces.
One method in the IEnumerator namely Current is not type safe because it
return an object
Assume also that I want to have type safe methods so I use generic interface
for IEnumerator<T>
My second question:
Because of method Current is not type safe it seems to be that it's enough
to
use the generic interface for IEnumerator<T> ? which mean I don't have to
use the
generic interface IEnumerable<T> but instead the the IEnumerable
Now to my third question below is an attempt to implement the two interfaces
IEnumerator<T> and IEnumerable in classSomeClass below.
Can you correct me because I will most certain have several errors ?
In this class I must implement four methods these are
MoveNext, Reset and Current for interface IEnumerator<T> and
one method GetEnumerator for interface IEnumerable
class SomeClass<T> : IEnumerable, where T : IEnumerator<T>
{
public bool MoveNext()
{...}
public void Reset()
{...}
public T Current()
{ return T; }
public IEnumerator GetEnumerator()
{ ...}
}
//Tony