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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:27:09+00:00 2026-05-26T13:27:09+00:00

Consider the following class structure: public class Foo<T> { public virtual void DoSomething() {

  • 0

Consider the following class structure:

public class Foo<T>
{
    public virtual void DoSomething()
    {
    }

    public class Bar<U> where U : Foo<T>, new()
    {
        public void Test()
        {
            var blah = new U();
            blah.DoSomething();
        }
    }
}

public class Baz
{
}

public class FooBaz : Foo<Baz>
{
    public override void DoSomething()
    {
    }
}

When I go to use the nested class, I have something like the following:

var x = new FooBaz.Bar<FooBaz>();

It seems redundant to have to specify it twice. How would I create my class structure such that I can do this instead:

var x = new FooBaz.Bar();

Shouldn’t there be some way on the where clause of the nested class to say that U is always the parent? How?


Update: Added methods for DoSomething() above to address some of the comments. It’s important that when I call DoSomething, it addresses the overridden version. If I just use Foo instead of U, then the base implementation is called instead.

  • 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-26T13:27:09+00:00Added an answer on May 26, 2026 at 1:27 pm

    If class Bar does not need to be generic, why do you make it one?

    This would work:

    public class Foo<T, U> where U : Foo<T, U>
    {     
        public class Bar
        {
            private T t;
            private U u;
        }
    }
    
    public class Baz
    {
    }
    
    public class FooBaz : Foo<Baz, FooBaz>
    {
    }
    

    And then

    var bar = new FooBaz.Bar();
    

    Of course all of this is totally abstract, so it might or might not apply to a practical example. What exactly are you trying to achieve here?

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

Sidebar

Related Questions

Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following code: class A { public: virtual void f() throw ( int
Consider the following class structure: class Filter { virtual void filter() = 0; virtual
Consider the following : class Abstract { public: virtual void func() = 0; };
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following class: class Something : ISomething { public void DoesSomething(int x) {
Consider the following class hierarchy: public class Foo { public string Name { get;
Consider the following class: public class Event<T> { public delegate void Handler<t>(t msg); private
Consider the following class hierarchy: base class Object with a virtual method foo() an
Conside the following code structure for android: package blah; class A{ class B{ public

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.