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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:02:33+00:00 2026-05-22T17:02:33+00:00

If I have an abstract class, is there any way I can return an

  • 0

If I have an abstract class, is there any way I can return an enumerator of the derived class’s type? Or would I have to use generics in the base class, or a generic method? Here’s a really dumbed down example of what I’m trying to do –

public abstract class Person {
    public IEnumerable<MyType> Search() {
        DbDataReader reader = Database.Instance.ExecuteReader(sql);
        while(reader.Read()) {
            MyType row = new MyType();
            row.Load(reader);
            yeild return row;
        }
    }

    private Load(DbDataReader reader) {
        //load instance from reader row
    }

    //declare properties that can be searched, such as Location
}

public class Programmer : Person {
    //declare properties that can be searched, such as Language
}

Then somewhere else I’d like to be able to call

Programmer programmer = new Programmer();
programmer.Location = "My city";
programmer.Language = "C#";
foreach(Programmer programmer in programmer.Search())
{
    //display list of c# programmers in my city
}

I know I can do this with a generic method, like Search<T>(), but I’d like to be able to call the search function from a class that does not know exactly type the Person is (for example, a base class for an AJAX handler)

If this cannot be done, can anyone give me an example or reason why not? Or would it just be too hard to implement into the compiler?

  • 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-22T17:02:33+00:00Added an answer on May 22, 2026 at 5:02 pm

    If you search an approach with this “pretty” usage I propose an ExtensionMethod. With an Ext you don’t have to define the effective type of the Entity twice. programmer.Search<Programmer>() => programmer.Search()

    public static PersonExtensions
    {
        public static IEnumerable<TType> Search<TType>(this TType row) : where TType : new(), Person 
        {
            DbDataReader reader = Database.Instance.ExecuteReader(sql);
            while(reader.Read()) {
                var row = new TType()
                row.Load(reader);
                yeild return row;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to require that a class have a particular abstract member?
Lets say I have an abstract base class and a derived class with some
Without any code in the subclasses , I'd like an abstract class to have
I have an interface IServiceInfo and an abstract class ServiceInfo. There are several classes
I have an abstract class called DatabaseRow that, after being derived and constructed, is
I have an abstract virtual base class Foo from which I derive many other
I would like to have my abstract parent class have a method that would
Okay, so let's say I have some random abstract base class Base and I
Please consider the following three .NET types: I have an interface, an abstract class,
Let say I have abstract class called: Tenant and Customer. The tenant in this

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.