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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:24:12+00:00 2026-05-24T00:24:12+00:00

I have a class, Container<T>, which has a ContainerContents<T>. The Container actually takes two

  • 0

I have a class, Container<T>, which has a ContainerContents<T>. The Container actually takes two type constraint parameters Container<TContainer,TContents> – TContainer being the type of the container, and TContents being the type of contents it accepts.

I want to ensure that if TContainer is X or derived from X, then TContents will also be X or derived from X, but that TContents does not have to equal TContainer.

I’m trying to express the following kinds of things.

  • Things that can be carried around (Swag), like a pencil.
  • Things that cannot be carried (BaseObject), like a tree.
  • Things that can hold other things (Container)
  • Containers that cannot be carried, like a bank vault.
  • Carriable Containers (like a backpack).

If a container can be carried, then its contents must be carriable too. But, just because the Container is a backpack doesn’t mean it can only carry backpacks.

I want to be able to code:

  • var ringWorld = new Container<BigRing, CivicWork>();
  • var pickleKnox = new Container<BankVault, Pickle>();
  • var swagBag = new Container<ToteBag, Swag>();
  • var tomeBag = new Container<ToteBag, Book>();

but not var treeBag = new Container<Bag, Tree>();

Here’s my skeletal setup.

public abstract class BaseObject
{
    private readonly string _name;

    protected BaseObject(string name)
    {
        _name = name;
    }

    public string Name { get { return _name; } }
}
public class Swag : BaseObject
{
    private readonly int _weight;

    public Swag(string name, int weight):base(name)
    {
        _weight = weight;
    }

    public int Weight { get { return _weight; } }
}
/* I like the flexibility of i.e.: Container<BankVault,Pickles> 
but if the container itself is carriable (Swag), then its contents 
are by nature also carriable. */

public class Container<TContainer,TContents> : BaseObject 
    where TContainer:BaseObject 
    where TContents:BaseObject, or Swag if TContainer:(Swag or derived from Swag)
{
    ContainerContents<TContents> _contents;

    public Container(string name, int maxItems):base(name)
    {

        /* if (TContainer is derived from Swag) { TContents must be too } */

        _contents = new ContainerContents<TContents>(maxItems);
    }
}
public class ContainerContents<T> : List<T> where T : BaseObject
{
    int _maxItems;

    public ContainerContents(int maxItems)
    {
        _maxItems = maxItems;
    }
}
  • 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-24T00:24:12+00:00Added an answer on May 24, 2026 at 12:24 am

    I don’t think this is going to work.

    I would create the following interfaces:

    interface ICarryable { }
    interface IContainer<T> { }
    

    Then you could implement the following classes:

    class Backpack<T> : ICarryable, IContainer<T>
    where T : ICarryable 
    {  }
    
    class Vault<T> : IContainer<T>
    {  }
    

    If a class implements ICarryable, it can be carried. If it doesn’t implement that interface, it is a fixed object that can’t be carried.
    This describes more precisely, what is going on. Your generic Container class doesn’t communicate, that the container is of type TContainer and its contents are of type TContainerContents.

    To avoid violating the DRY principle, you can still create a generic container base class, your vault and backpack inherit from. Making it abstract ensures, that no one uses it instead of your concrete implementations.

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

Sidebar

Related Questions

I have a collection which contains two type of objects A & B. Class
Basically, I have a Container class called Employees which has in it an ArrayList.
I have an solution in VS 2008 which contains two class library projects and
My situation is essentially this: I have a class called Foo which has dependencies
I have a custom container class for which I'd like to write the iterator
I have two SWF files which I shall call container and slave . The
Let's say I have a container class called myList . This container class has
I have a class that has in its constructor some primitive type arguments, such
I have a class A which has an reference to an object of class
The problem: I have a class which contains a template method execute which calls

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.