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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:40:26+00:00 2026-05-29T14:40:26+00:00

Assuming class A { } class B : A { } covariance is not

  • 0
  • Assuming

    class A
    { }

    class B : A
    { }

covariance is not supported for generic class.

Meaning – we cant do something like this :

MyConverter<B> x1= new MyConverter<B>();
MyConverter<A> x2= x1;  

Thats fine and understood.

From my reading – i understand that Covariance will be available:

“If you use a backing Generic Interface Being implemented on a Generic Class – so that access to the T type object instance will be available through those interfaces “.

I have just one problem.

Ive seen many examples of the “converter” class as a form of Stack .

But never understood ” what if I want to use only 1 instance of B from a reference of A ? “

so Ive tried some code :

Create B object + values —> use Generic Converter for B —>
use the covariance flow to get its A reference —> now you can use it
either as A or as B.

enter image description here

enter image description here

My question :

Is That the correct way of doing this ( for using covariance for 1 object only ) ?

p.s.
The code is working and compiled ok. https://i.stack.imgur.com/PJ6QO.png

Ive been asking /reading a lot about this topic lately – I dive into things in order to understand them the best I can.

  • 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-29T14:40:27+00:00Added an answer on May 29, 2026 at 2:40 pm

    Your code compiles and works, so is it “correct”? I guess it is!

    However it is not very interesting having a stack that only contains a single element; that’s not really a stack. Let’s think about how you might make a truly covariant and contravariant stack.

    interface IPush<in T> { void Push(T item); }
    interface IPop<out T> { T Pop(); }
    class Stack<T> : IPush<T>, IPop<T>
    {
        private class Link
        {
            public T Item { get; private set; }
            public Link Next { get; private set; }
            public Link(T item, Link next) { this.Item = item; this.Next = next; }
        }
    
        private Link head;
        public Stack() { this.head = null; }
    
        public void Push(T item)
        {
            this.head = new Link(item, this.head);
        }
    
        public T Pop()
        {
            if (this.head == null) throw new InvalidOperationException();
            T value = this.head.Item;
            this.head = this.head.Next;
            return value;
        }
    }
    

    And now you can use the stack covariantly for popping, and contravariantly for pushing:

    Stack<Mammal> mammals = new Stack<Mammal>();
    IPop<Animal> animals = mammals;
    IPush<Giraffe> giraffes = mammals;
    IPush<Tiger> tigers = mammals;
    giraffes.Push(new Giraffe());
    tigers.Push(new Tiger());
    System.Console.WriteLine(animals.Pop()); // Tiger
    System.Console.WriteLine(animals.Pop()); // Giraffe
    

    What if I want to use only one instance of B from a reference of A?

    Your question is “what if I want to use a Tiger but I have a reference an Animal?” The answer is “you can’t” because the Animal might not be a Tiger! If you want to test whether the reference to Animal is really a tiger then say:

    Tiger tiger = myAnimal as Tiger;
    if (tiger != null) ...
    

    or

    if (myAnimal is Tiger) ...
    

    What about if you want to convert class C<B> to C<A>?

    That’s not possible. There is no reference conversion there. The only covariant and contravariant reference conversions in C# 4 are on generic interfaces and generic delegates that are constructed with reference types as the type arguments. Generic classes and structs may not be used covariantly or contravariantly. The best thing you can do is make the class implement a variant interface.

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

Sidebar

Related Questions

Assuming I have an Entity Framework 4.2 class like this: class Company { public
Assuming I have a class like this: class Database { public static $hostname =
Assuming I have only the class name of a generic as a string in
Assuming that I have a class named Class , And I would like to
Assuming I have a class like public class FooImpl { public void bar(){}; }
Without automatic reference counting you often write code like this, when adding a new
Assuming the class public class Foo { public List<Bar> Bar = new List<Bar>(); public
Assuming a class called Bar in a namespace called foo , which syntax do
Assuming public class MyClass { public int ID {get; set; } public string Name
Assuming the following class stub: public class Foo<T> { private Class<T> type; public Foo<T>

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.