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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:15:27+00:00 2026-05-29T07:15:27+00:00

if i have : public class A { public int a1;} public class B

  • 0

if i have :

   public class A {   public int a1;}
   public class B : A {  public int b1;}

   void myFunc(A a) 
   {}

  static void Main(string[] args)
       {
         B b = new B();
         myFunc(b);
       }

in myFunc , a can access b object but it can reference only (without cast) to a region in memory which is type A.

that is understood.

HOwever in covariance it seems that a can also access b :
enter image description here

As you can see – it accepts Enumerable of A and it still can access its B typed objects


questions:

1) Ok, How behind the scenes it is working ? how can an A reference can show me a larger object ?

2) What if i wanted to see in the function the a1 property from the A class ?what should I change ?

edit

covariance related:

Before C# 4, you couldn’t pass in List:
cannot convert from
‘System.Collections.Generic.List’ to
‘System.Collections.Generic.IEnumerable’

Covariance and contravariance real world example

  • 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-29T07:15:28+00:00Added an answer on May 29, 2026 at 7:15 am

    How can an A reference can show me a larger object?

    First off, it is a smaller type. Every giraffe is an animal but not every animal is a giraffe. Therefore there are fewer giraffes in the world than there are animals in the world. Therefore giraffe is a smaller type than animal.

    Your type B is a smaller type than A. And of course a reference to a larger type can refer to something of a smaller type.

    That has nothing to do with covariance. It is always the case that an IEnumerable<A> can give you a B:

    List<A> myList = new List<A>() { new B(); } // No covariance here
    Console.WriteLine(myList[0].GetType()); // it's a B.
    

    A list of animals can contain a giraffe. That has nothing to do with covariance.

    Similarly, a reference can always give you a smaller type back:

    A a = new B(); // Legal!
    

    to those who says it has nothing to do with covariance…

    That a sequence of A can contain a B has nothing to do with covariance. What has to do with covariance is that a sequence of B can be converted to a sequence of A by reference conversion. Before covariant conversions were added to C# 4, that conversion would have failed.

    What if i wanted to see in the function the a1 property from the A class? what should I change?

    You shouldn’t change anything; it already works. Try it.

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

Sidebar

Related Questions

I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have a class : class X{ public : void f ( int )
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
I have : public class Person : INotifyPropertyChanged { private string _name; public int
Suppose I have: public class OuterClass() { public class InnerClass { public void someMethod(int
Suppose I have: public class foobar { public int lorem; public int ipsum; }
More specifically, if I have: public class TempClass : TempInterface { int TempInterface.TempProperty {
Say I have this class: public class Account { public int AccountID { get;
I have a class: public class User { public virtual int Id { get;

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.