Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6189517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:25:42+00:00 2026-05-24T02:25:42+00:00

Suppose I have an abstract base class BaseTimeCollection and at least one concrete class

  • 0

Suppose I have an abstract base class BaseTimeCollection and at least one concrete class ConcreteTimeCollection which inherits from the base class.

I would like my base class to inherit from ICollection<T>.

Inheriting from ICollection<T> requires me to provide implementations for a number of methods, including IEnumerable.GetEnumerator() and IEnumerable<T>.GetEnumerator.

I do not want to implement these methods in BaseTimeCollection – instead I would prefer to implement them individually in each of my concrete classes.

This is where I run into trouble.

The GetEnumerator methods need to be declared explicitly, because there are two of them both with the same name but different return types.
However, it seems that as soon as I make the signature explicit I can no longer use the abstract modifier. Essentially I am forced to implement the GetEnumerator method in my base class.

public abstract class BaseTimeCollection : ICollection<Time>
{
    abstract IEnumerator IEnumerable.GetEnumerator();  // compile error: The modifier    'abstract' is not valid for this item
    abstract IEnumerator<Time> IEnumerable<Time>.GetEnumerator(); // compile error: The       modifier 'abstract' is not valid for this item
}

public class ConcreteTimeCollection : BaseTimeCollection
{
    IEnumerator IEnumerable.GetEnumerator()
    {
       // this is where I would like to provide my implementation for IEnumerable.GetEnumerator()
    }

    IEnumerator<Time> IEnumerable<Time>.GetEnumerator()
    {
        // this is where I would like to provide my implementation for IEnumerable<Time>.GetEnumerator()
    }
}
  1. What have I missed?

  2. Is there some way I can defer implementation of the GetEnumerator methods to the concrete classes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:25:43+00:00Added an answer on May 24, 2026 at 2:25 am

    You can easily defer the implementation to your child classes by having the explicit implementations call protected abstract methods in your abstract class and then letting the children implement those abstract methods:

    public abstract class BaseTimeCollection : ICollection<Time>
    {
        protected abstract IEnumerator IEnumerable_GetEnumerator();
        protected abstract IEnumerator<Time> GenericEnumerable_GetEnumerator();
    
        IEnumerator IEnumerable.GetEnumerator() 
        { 
            return IEnumerable_GetEnumerator(); 
        }
    
        IEnumerator<Time> IEnumerable<Time>.GetEnumerator()
        {
            return GenericEnumerable_GetEnumerator();
        }
    }
    

    Sorry for the poor naming scheme…it’s the best I could come up with this early in the morning.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have abstract class A and abstract class B which inherits from A,
Suppose we have an abstract class Element from which classes Triangle and Quadrilateral are
Suppose I have an abstract base class, that just defines a container on which
I have created an abstract base class Animal which has public virtual abstract method
Suppose I have class hierarchy like the one shown in picture. Suppose I need
Suppose we have abstract class A (all examples in C#) public abstract class A
I have an abstract immutable base class that defines enforces child classes to be
Suppose I have the following abstract class: abstract class A (var is_happy : Boolean)
(edit) more info. First notice new virtual. This class inherits a base class which
Suppose I have the following trait with two abstract vals trait Base { val

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.