471,319 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Implementing IEnumerable<T> in as abstract

I am developing an abstract class which implements IEnumerable<T>. I need
the actual implemented methods here to be abstract as well - they will be
implemented by MY subclasses.

However, I cannot seem to get the explicit interface method to accept being
abstract.

Take the following simple class:

abstract class TestEnumerable : IEnumerable<string>
{
public abstract IEnumerator<string> GetEnumerator();

abstract IEnumerator System.Collections.IEnumerable.GetEnumerator();
}

When compiling, I get "The modifier 'abstract' is not valid for this item."
on the second GetEnumerator. Any ideas?

--
Adam Clauss
Dec 10 '05 #1
3 6019
Since IEnumerator<string> derives from the non-generic IEnumerator
interface, and you already have an abstract method that returns the
generic enumerator, you can just write a simple implementation for the
second version instead of making it abstract:

public abstract IEnumerator<string> GetEnumerator();

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

Jesse

Dec 10 '05 #2
Huh... wow.

Yeah... I guess I hadn't gotten as far as thinking how I would ACTUALLY
implement the two methods yet :) Only that it would be determined by
subclasses.

Thanks for pointing out the easy-way-out!

--
Adam Clauss

"Jesse McGrew" <jm*****@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Since IEnumerator<string> derives from the non-generic IEnumerator
interface, and you already have an abstract method that returns the
generic enumerator, you can just write a simple implementation for the
second version instead of making it abstract:

public abstract IEnumerator<string> GetEnumerator();

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

Jesse

Dec 10 '05 #3
"Adam Clauss" <ca*****@no.spam.gmail.com> a écrit dans le message de news:
OW**************@TK2MSFTNGP09.phx.gbl...

|I am developing an abstract class which implements IEnumerable<T>. I need
| the actual implemented methods here to be abstract as well - they will be
| implemented by MY subclasses.
|
| However, I cannot seem to get the explicit interface method to accept
being
| abstract.
|
| Take the following simple class:
|
| abstract class TestEnumerable : IEnumerable<string>
| {
| public abstract IEnumerator<string> GetEnumerator();
|
| abstract IEnumerator
System.Collections.IEnumerable.GetEnumerator();
| }
|
| When compiling, I get "The modifier 'abstract' is not valid for this
item."
| on the second GetEnumerator. Any ideas?

Assuming that you want to use explicit interface implementation to hide any
public methods, then you can do the following :

abstract class TestEnumerable : IEnumerable<string>
{
protected abstract IEnumerator<string> GetEnumerator();

IEnumerator IEnumerable<string>.GetEnumerator()
{
return GetEnumerator();
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Gustaf | last post: by
5 posts views Thread by Shikari Shambu | last post: by
2 posts views Thread by Morgan Cheng | last post: by
1 post views Thread by shapper | last post: by
2 posts views Thread by Berryl Hesh | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.