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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:28:25+00:00 2026-05-18T23:28:25+00:00

I am making some abstract base classes. The base object is defined as: public

  • 0

I am making some abstract base classes. The base object is defined as:

    public abstract class Bar
    {
        public bool Enabled;
    }

The base collection is defined as:

 public abstract class Foo<T> : List<T>
        where T : Bar
    {
        public Foo<T> GetAllEnabled()
        {
            Foo<T> ret = new Foo<T>();

            foreach (T item in this)
            {
                if (item.Enabled)
                {
                    ret.Add(item);
                }
            }

            return ret;
        }
    }

Naturally, I get an error trying do create a new Foo<T>(), because Foo is an abstract class. Without the abstract modifier, everything works fine, but I want the modifier there because I want to make sure the class gets derived.

I have tried

     Type U = this.GetType();
        Foo ret = new U();


but I get an error "The type or namespace name 'U' could not be found (blah blah blah)".

I think my attempted fix should work, and I must just have the syntax wrong. What am I missing here?

  • 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-18T23:28:25+00:00Added an answer on May 18, 2026 at 11:28 pm
    Type U = this.GetType();
    Foo ret = new U();
    

    Type U will contain information on the data type, it is not the equivalent of a generic type parameter that you can then instantiate. You can use U to create a type through Activator.CreateInstance, which has a few overloads and accepts Type objects, but then you’re left with needing to cast the resulting object to an appropriate type in order to be able to use it.

    Type type = this.GetType();
    Foo<T> ret = (Foo<T>)Activator.CreateInstance(type);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A base project contains an abstract base class Foo. In separate client projects, there
I'm working through some sample code and then this appeared: public abstract class RandomPool<T>
Here is the question's plot: suppose I have some abstract classes for objects, let's
Consider the following code: object foo { trait Bar[Q[_]] implicit object OptionBar extends Bar[Option]
I am making an abstract class to ease up handling of properties. Now I
I have a abstract class called Customer which has some attributes shared amongst all
After making some changes in my models (eg. new field in a model and
I'm making some changes to a legacy classic ASP application. I've made the changes
I am programatically making some LinkButtons, and they worked fine for a while, but
I am just in the process of making some little demo tutorial videos for

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.