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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:07:27+00:00 2026-05-17T03:07:27+00:00

In C# I have an intrusive tree structure that looks like this: public abstract

  • 0

In C# I have an intrusive tree structure that looks like this:

public abstract class Node
{
    Container parent;
    Node nextNode;
    Node previousNode;

    public abstract class Container : Node
    {
        Node firstChild;
        Node lastChild;
    }
}

The various objects that can be added to the tree inherit from either Node or Container depending on whether they can have children or not.

By making Container an inner class it means that it can access the private members in Node for managing the container’s list of children.

This is all well and good. But now I wish to make it generic so that I can reuse it while maintaining type safety – basically moving all the tree functionality to a generic class above Node and another between Node and Container. Here’s a rough design of what I’m trying to do:

public abstract class GenericNode<Node, Container>
    where Node : GenericNode<Node, Container>
    where Container : GenericNode<Node, Container>.GenericContainer
{
    Container parent;
    Node nextNode;
    Node previousNode;

    public abstract class GenericContainer : Node
    {
        Node firstChild;
        Node lastChild;
    }
}

Which, of course, doesn’t work because you can’t make GenericContainer inherit from Node (compiler error CS0689). Even if I drop the inner class requirement (say, by using internal and just being careful in my own library) I still can’t figure out a design that doesn’t run into the same problem (and error).

(I didn’t think I would have to, but just to spell it out: I am not trying to “fix” the compile error, nor am I looking for a simple tree implementation. This is a container design question.)

And so now I’m a bit stumped. Does anyone have any better ideas about how to design this thing?

Edit: Be sure to take a look at this answer, which is another stab at the design, that attempts to use extension methods to avoid the problem of “injecting” a class into the inheritance hierarchy (but unfortunately doesn’t fully work).

  • 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-17T03:07:27+00:00Added an answer on May 17, 2026 at 3:07 am

    Following your extension method approach, what if you define the inheritance constraint (between Node and Container) on an interface instead, and decorate container classes with the interface.

    {
        MyNode n = new MyNode();
        var c = new MyNode.MyContainer();
        c.AddChild(n);
    
        MySubNode s = new MySubNode();
        c.AddChild(s);
    
        OtherNode o = new OtherNode();
        o.AddChild(o);
    
        //compiler doesn't allow this, as you'd expect:
        //c.AddChild(o);
    }        
    
    public interface IContainer<TContainerType, TNodeType>
        where TNodeType : GenericNode<TContainerType, TNodeType>
        where TContainerType : TNodeType, IContainer<TContainerType, TNodeType>
    {
    }
    
    public static class ContainerExtensions
    {
        public static void AddChild<TContainerType, TNodeType>(this IContainer<TContainerType, TNodeType> self, TNodeType node)
            where TNodeType : GenericNode<TContainerType, TNodeType>
            where TContainerType : TNodeType, IContainer<TContainerType, TNodeType>
        {
            GenericNode<TContainerType, TNodeType>.AddChild(self as TContainerType, node);
        }
    }
    
    public class GenericNode<TContainerType, TNodeType>
        where TNodeType : GenericNode<TContainerType, TNodeType>
        where TContainerType : GenericNode<TContainerType, TNodeType>
    {
        TContainerType parent;
        TNodeType nextNode;
        TNodeType previousNode;
    
        // Only used by Container
        TNodeType firstChild;
        TNodeType secondChild;
    
        internal static void AddChild(TContainerType container, TNodeType node)
        {
            container.firstChild = node;
            node.parent = container;
        }
    }
    
    public class MyNode : GenericNode<MyContainer, MyNode>
    {        
    }
    
    public class MyContainer : MyNode, IContainer<MyContainer, MyNode>
    {
    }
    
    public class MySubNode : MyNode
    {
    }
    
    public class OtherNode : GenericNode<OtherNode, OtherNode>, IContainer<OtherNode, OtherNode>
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a setup that looks like this. class Checker { // member data
I have an abstract base class for a pointAccumulator. This abstract base will be
I have an application where the memory profile looks something like this: (source: kupio.com
I have a process intensive task that I would like to run in the
Let's say we have a memory-intensive class like an Image , with chainable methods
I'm currently working on a simple way to implement a intrusive tree structure in
Background Let's say I have the following class: public class MyValue<T> { public T
So I have this extremely memory-intensive Java application I'm building, which builds a tree
At work we have a base class, let's call it IntrusiveBase, that acts something
I have this SearchMenuActivity that has a bunch of options the user can select.

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.