473,246 Members | 1,449 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,246 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 6324
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
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
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...

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.