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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:50:20+00:00 2026-05-25T15:50:20+00:00

I have small doubt in CoVariance and ContraVariance.. See the Following Code.. interface IGetElement<out

  • 0

I have small doubt in CoVariance and ContraVariance..
See the Following Code..

interface IGetElement<out T>
{
    int Counter { get; }
    T GetNext();
}

interface IAddElement<in T>
{
    void Add(T t);
}

class Collection<T> : IAddElement<T>, IGetElement<T> where T : Fruit
{
    List<T> tList = new List<T>();

    private int _counter = 0;

    public int Count { get { return tList.Count; } }

    public void Add(T t)
    {
        tList.Add(t);
    }

    int IGetElement<T>.Counter
    {
        get { return _counter; }
    }

    public T GetNext()
    {
        return tList[_counter++];
    }

    public void Rset()
    {
        _counter = 0;
    }
}

abstract class Fruit
{
    public abstract void Taste();
}

class Apple : Fruit
{
    public override void Taste()
    {
        Console.WriteLine("Like Apple");
    }
}

This is the sample code.. And now the Client is

 static void Main(string[] args)
 {
        IGetElement<Fruit> covarience = new Collection<Apple>(); // CoVarience..

        IAddElement<Apple> contravarience = new Collection<Fruit>();//ContraVarience.. Compiling fine and working also fine... :)

        IGetElement<Fruit> fruits = new Collection<Apple>();

        IAddElement<Fruit> apples2 = fruits as IAddElement<Apple>;//Here its Compiler error : Cannot implicitly convert type 'Test.IAddElement<Test.Apple>' to 'Test.IAddElement<Test.Fruit>'. An explicit conversion exists (are you missing a cast?)  

        IAddElement<Apple> apples1 = fruits as IAddElement<Fruit>;//This is not posible  
        /* Why this is not possible..? In this case No Compiler Error But Run Time error ie., 
       apples1 is NULL.. I don't know why.. because.. in fruits cantains apple only but 
       It is unable it caste it as ContrVariantly..  ?------------1 */

        IAddElement<Apple> apples = fruits as IAddElement<Apple>;//This is posible 


        IGetElement<Fruit> f = apples as IGetElement<Apple>;//This is posible.. 
        /* Why this is posible.. ? here we are casting t as CoVariantly..
        If ------------1 is not posible how this would be posible... yes.. I am casting to    
        actual object which is not Null.. ? -----------2 */
  }

Please answer my Question in Commented Source code… 🙂 ——–1,———2.

Thanks And Regards,
Dinesh

  • 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-25T15:50:21+00:00Added an answer on May 25, 2026 at 3:50 pm

    To expand a bit on James’ answer:

    1. Why this is not possible? In this case there is no compiler error but at runtime apples1 is null. I don’t know why.

    Local variable fruits at runtime refers to an object of type Collection<Apple>. That is, a collection which contains only apples.

    You are asking “can I add any fruit to this collection?” No. You can only add apples, not any fruit. Therefore the result is null.

    The reason this fails at runtime and not at compile time is because the compile-time type of fruits is an interface type, and you are converting it to a different interface type. Any two interfaces might be implemented by a given object; the compiler does not do flow analysis to determine that fruits is only ever assigned one particular type. Therefore the check cannot be done until runtime.

    1. Why this is posible? Here we are converting covariantly.

    There are two conversions here. First apples is converted to IGetElement<Apple> and then that is covariantly converted to IGetElement<Fruit>.

    The first conversion succeeds because local variable apples refers to a Collection<Apple> which implements IGetElement<Apple>. You are asking “is this object something that can hand me an apple?” and the answer is “yes”.

    The second conversion succeeds because the compiler knows that “an object that can hand me an apple” can be safely treated as “an object that can hand me a fruit”.

    Is that now clear?

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

Sidebar

Related Questions

I have a small doubt I have following code bool res= false; if(cond1) {
i have small doubt.why the below code is printing value i=2. int i=2; i=i++;
I have a small architecture doubt about organizing code in separate functional units (most
I have a small doubt, how to manage the following situation. In a DB
I have been using Hibernate/Spring. I have a small doubt. If we get 1000
I'm having a small doubt in shell Scripting I have a program (a.out) which
I have small doubt in python. x = '' if x: print 'y' so,
I am new to iphone.I have a small doubt (i.e),I have create a table
Am Beginner to java.I have a small doubt while i am developing an application
I have small query today I happen to see when I typed a website

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.