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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:26:02+00:00 2026-05-26T08:26:02+00:00

I have a sample program with a base Fruit class and a derived Apple

  • 0

I have a sample program with a base Fruit class and a derived Apple class.

class Testy
{
    public delegate void FruitDelegate<T>(T o) where T : Fruit;

    private List<FruitDelegate<Fruit>> fruits = new List<FruitDelegate<Fruit>>();

    public void Test()
    {
        FruitDelegate<Apple> f = new FruitDelegate<Apple>(EatFruit);

        fruits.Add(f); // Error on this line
    }

    public void EatFruit(Fruit apple) { }
}

I want to have a list of fruit delegates and be able to add delegates of more derived fruit to the list. I believe this has something to do with covariance or contravariance but I cannot seem to figure it out.

The error message is (without namespaces):

The best overloaded method match for 'List<FruitDelegate<Fruit>>.Add(FruitDelegate<Fruit>)' has some invalid arguments`
  • 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-26T08:26:02+00:00Added an answer on May 26, 2026 at 8:26 am

    A FruitDelegate<Fruit> is a delegate that accepts any fruit. For example, the following is valid:

    FruitDelegate<Fruit> f = new FruitDelegate<Fruit>(EatFruit);
    f(new Apple());
    f(new Banana());
    

    You can make the type parameter T of FruitDelegate<T> contravariant:

    public delegate void FruitDelegate<in T>(T o) where T : Fruit;
    

    which allows you to assign a FruitDelegate<Fruit> instance to a FruitDelegate<Apple> variable:

    FruitDelegate<Apple> f = new FruitDelegate<Fruit>(EatFruit);
    f(new Apple());
    

    This is valid, because the delegate refers to a method that (among other fruits) accepts apples.

    However, you cannot assign a FruitDelegate<Apple> instance to a FruitDelegate<Fruit> variable:

    FruitDelegate<Fruit> f = new FruitDelegate<Apple>(EatApple); // invalid
    f(new Apple());
    f(new Banana());
    

    This is invalid, because the delegate should accept any fruit, but would refer to a method that accepts no fruit other than apples.

    Conclusion: you cannot add a FruitDelegate<Apple> instance to a List<FruitDelegate<Fruit>>, because a FruitDelegate<Apple> is not a FruitDelegate<Fruit>.

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

Sidebar

Related Questions

I have a base class Element and derived two other classes from it, Solid
I have a sample program where I have a class called ObserverTest where I
I have written some sample program and DLL to learn the concept of DLL
Below I have written a sample program that I have written to learn about
Does anyone have sample code to copy open (in-use and locked by another program)
I have this class that has a static member. it is also a base
Well its simple here you have my vb.net code: Public Class Class1 Public Overridable
Given a base class that is inherited by plethora of derived classes, and a
I have a simple class that stops and starts a timer when my program
Say I have simple program that emulates a board game with a number of

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.