473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 6340
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Doug Dew | last post by:
This won't compile: using IEnumerable<T> = System.Collections.Generic.IEnumerable<T>; namespace MyNamespace { public class MyClass<T> : IEnumerable<T> { // Appropriate stuff here }
15
by: Gustaf | last post by:
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...
5
by: Shikari Shambu | last post by:
Hi, I am trying to implement a collection that implements IEnumerable<T>. I keep getting the following error 'IEnumerator<...>.Current' in explicit interface declaration is not a member of...
2
by: Morgan Cheng | last post by:
In .Net 2.0, Generics are introduced. I found that IEnumerable<T> inherites from IEnumerable. This makes implementation of IEnumerable<Thas to have two GetEnumerator methods defined( one for...
0
by: Andrus | last post by:
I noticed that Linq-SQL DataContext ExecuteQuery method does not have new() constraint: IEnumerable<TResultExecuteQuery( command, ... ) IEnumerable must return object so it must create this...
2
by: Tony Johansson | last post by:
Hello! Below I have a working program. I have one generic class called Farm<T> with this header definition public class Farm<T: IEnumerable<Twhere T : Animal Now to my question I changed the...
1
by: shapper | last post by:
Hello, On my MVC projects I often create classes which contains properties which are lists of other classes. Should I start using IQueryable<Tor IEnumerable<Tinstead of List<T>? What are...
5
by: Anders Borum | last post by:
Hello I'm developing an API with a number of concrete collections that inherit from an abstract collection hierarchy. Each inheritance level provides an implementation of IEnumerable<T>, thus...
2
by: Berryl Hesh | last post by:
Converting in the other direction , IEnumerable<Interfaceto List<ImplInterface>, I can just create a new List<ImplInterface(data) where data is IEnumerable<Interface>. How can I convert the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.