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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:48:28+00:00 2026-05-29T12:48:28+00:00

I’ve been busy with C# 4.0 generics, and now I basically want to do

  • 0

I’ve been busy with C# 4.0 generics, and now I basically want to do something like this:

public abstract class GenericTree<T> : Tree
    where T : Fruit
{
    public Tree(IFruitCollection<T> fruits)
        : base(fruits) { }
}

The base Tree class looks like this:

public abstract class Tree
{
    private IFruitCollection<Fruit> fruits;

    public IFruitCollection<Fruit> GetFruits
    {
        get { return fruits; }
    }

    public Tree(IFruitCollection<Fruit> fruits)
    {
        this.fruits = fruits;
    }
}

Here is my first problem. The constructor of GenericTree can’t cast the the generic collection to a fruit collection. I’ve also got an implementation of the GenericTree:

public class AppleTree : GenericTree<Apple>
{
    public AppleTree()
        : base(new FruitCollection<Apple>) { }
}

Here’s my second problem. When I add fruit to an instance of AppleTree, using myAppleTree.GetFruits.Add(…), I am not restricted to apples only. I am allowed to add all kinds of fruit. I don’t want this.

I tried to solve that problem by adding this to the GenericTree:

new public IFruitCollection<T> GetFruits
{
    get { return base.GetFruits as IFruitCollection<T>; }
}

But this is not possible either. It somehow always returns null. It could be possible that this will be solved, when my first problem is solved.

The IFruitCollection interface looks like this:

public interface IFruitCollection<T> : ICollection<T>
    where T : Fruit { ... }

And the FruitCollection class is a simple implementation of the Collection class.
Oh, and of course, the Apple class extends the Fruit class.

The solution is to make the IFruitCollection interface compatible with both covariance and contravariance. But how do I achieve this? The “in” or “out” parameter keywords are not possible, because the ICollection interface doesn’t allow it.

Thanks a lot for your help!

  • 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-29T12:48:44+00:00Added an answer on May 29, 2026 at 12:48 pm

    I think I found my solution, while I was trying out phoog’s workaround. Thanks, phoog!

    At first I didn’t give GenericTree his own FruitCollection. Mosty because the base class often loops through his own collection, for example to update the fruits. This resulted into fruits not updating, because they were added to the GenericTree’s collection, not to the base collection.

    But eventually I realised that casting those collections is never going to work either.

    Now I’ve made another FruitCollection class that automatically adds and removes components to the base FruitCollection, and vice versa. This solution works great for me!

    public FruitCollection<T, U> : FruitCollection<T>
        where T : U
        where U : Fruit
    {
        private FruitCollection<U> baseCollection;
    
        public FruitCollection(FruitCollection<U> baseCollection)
            : base()
        {
            this.baseCollection = baseCollection;
    
            // here I added code that throws events whenever the collection is changed
            // I used those events to add/remove fruit to the base collection, and vice versa
            // see the link below.
            ...
        }
    
        ...
    }
    
    public class GenericTree<T>: Tree
        where T : Fruit
    {
        private FruitCollection<T> fruits;
    
        // use the new keyword to hide the base collection
        new public FruiCollection<T> Fruits
        {
            get { return fruits; }
        }
    
        public GenericTree()
            : base()
        {
            // after hiding the base collection, use base.Fruits to get it
            fruits = new FruitCollection<T, Fruit>(base.Fruits);
        }
    }
    

    This helped me: How to handle add to list event?

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
Basically, what I'm trying to create is a page of div tags, each has

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.