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 7011895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:11:06+00:00 2026-05-27T22:11:06+00:00

I have an example where I want an abstract class interface to return something

  • 0

I have an example where I want an abstract class interface to return something like this

abstract class AnimalProcessor {
    public abstract IList<Animal> ProcessResults();
}

Then the concrete examples

class GiraffeProcessor : AnimalProcessor {
    public override IList<Animal> ProcessResults() {
        return new List<Giraffe>();
    }
}

class LionProcessor : AnimalProcessor {
    public override IList<Animal> ProcessResults() {
        return new List<Lion>();
    }
}

The problem is that the concrete classes need to have the same signature to override the ProcessResults() method so they need to return an IList<Animal>, however the ACTUAL data I want to return is an IList<Lion>, IList<Giraffe> etc, but then the calling code has to do

GiraffeProcessor processor = new GiraffeProcessor();
IList<Animal> results = processor.GetResults();

Which does not give me an Ilist which is what I want.

Problems

1) Above code does not compile. The giraffeProcessor has to return a concrete List<Animal>, you can populate it with Giraffe objects but the object type you construct to return has to be List<Animal>. Not ideal.

2) When you return the results, you can only get an IList<Animal>, not IList<Giraffe>. I have tried casting explicitly to IList<Giraffe> with
IList<Giraffe> results = (IList<Giraffe>) processor.GetResults();
which gives a runtime error, presumably because the object returned is NOT an IList<Giraffe>, it is an IList<Animal> which CONTAINS Giraffe objects.

Can anyone suggest what I am doing wrong here with my design as Im a bit stumped as to the best way to accomplish this.

  • 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-27T22:11:07+00:00Added an answer on May 27, 2026 at 10:11 pm

    How about:

    abstract class AnimalProcessor<T> where T : Animal {
        public abstract IList<T> ProcessResults();
    }
    
    class GiraffeProcessor : AnimalProcessor<Giraffe> {
        public override IList<Giraffe> ProcessResults() {
            return new List<Giraffe>();
        }
    }
    
    class LionProcessor : AnimalProcessor<Lion> {
        public override IList<Lion> ProcessResults() {
            return new List<Lion>();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code structure: public abstract class ContentEntryBase { public string UniqueIdentifier; public
I have created a separate class (let's call it class2.cs for example) and want
I have an abstract class A and several implementations of it. I expect this
I have an abstract class, AbsClass that implements an interface, IClass . IClass has
i want to create an abstract representation of an existing object. For this example
I have an abstract base class which acts as an interface. I have two
I have an abstract class: abstract class AbstractDataExport { public string name; public abstract
In this example: class A { public A() { // pre-init1 // post-init1 }
I have interface IDoc and an abstract class that implements it named Doc. I
I have an abstract base class from which many classes are derived. I want

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.