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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:44:21+00:00 2026-05-28T18:44:21+00:00

This is such a difficult question to explain online but I shall try my

  • 0

This is such a difficult question to explain online but I shall try my best. I have instantiated an object of type B which is stored in a variable of type A. I use the get type property, so now it is type B. Have I therefore performed an implicit conversion of type A and B. So therefore when a.show() is called is it of type B?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{

    class A
    {
        public virtual void show()
        {
            Console.WriteLine("Showing A");
        }

        public void test()
        {
            Console.WriteLine("called from A");
        }
    }

    class B:A
    {
        public override void show()

        {
            Console.WriteLine("Showing B");
        }

        public void testb()
        {
            Console.WriteLine("called from B");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {


            A a = new B();
            // Outputs ConsoleApplication.B
            Console.WriteLine("{0}", a.GetType());
            // outputs showing B
            a.show();
            // outputs called from A
            a.test();

            Console.ReadLine();
        }
    }
}
  • 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-28T18:44:22+00:00Added an answer on May 28, 2026 at 6:44 pm

    You haven’t performed an implicit conversion as you haven’t actually converted anything.

    A a = new B();
    

    You have directly instanciated an object of type B. You can reference it from a variable of type A because B is a sub class of A. However, the two actions should be considered as independant.

    … = new B();

    will always instanciate an object of type B, regardless of what is on the left.

    A a = ...
    

    Will always try to assign what is on the right to the variable a.

    The only difference that the type of the reference will make is in how the object is seen by the CLR.

    A a = new B();
    B b = new B();
    

    Both are objects of type B. However, ‘a’ will only allow you to work with it as if it were an object of type A. So, you can only call a.show() and a.test(), even although it is actually of type B and so does have the additional method testb(). ‘b’ will allow you to call b.show(), b.test() and b.testb(). Regardless of the variable type, when you call show(), it is of type b and so it is the overriden show() method that is returned.

    Finally, what you can now do is downcast. i.e. you instanciated an object of type B so you can now cast it to a variable of type b and so allow full access to all of type B members.

    e.g.:

    A a = new B();
    a.testb(); // this will not compile as a does not have a definition of testb().
    
    A a = new B();
    B b = (B)a; // downcast a to a reference of type B
    b.testb(); // this is now fine
    

    I just saw you additional question of why would you want to instanciate something as type A, i.e. the base class. This is to provide a common ground as a way of working with objects of different types. For example, you may have a base type of Animal and derrive from it several sub classes – Cat, Dog, Gerbil. However, you may then want to be able to pass a list of all Animals to a method that only needs to work with common properties. So, if Animal had a name, your List could be iterated and each Animals name referred to, even although Animal could itself be Abstract (Not able to be instanciated) and the list comprise entirely of Dogs, Cats and Gerbils. You can also then downcast the various animals by finding out what there type is to interact with them further, e.g.

    if (animal is Dog) {
        Dog dog = (Dog)animal;
    }
    

    Or you could use the ‘as’ keyword to do the cast:

    Dog dog = animal as Dog;
    

    Hope this helps.

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

Sidebar

Related Questions

The question is a bit difficult to explain, but I will try. I have
Following this question Wikipedia says: http://en.wikipedia.org/wiki/First_normal_form#Repeating_groups_within_columns A query such as Which pairs of customers
I saw variants of this question before, but didn't find answer yet. I have
This is probably an extremely difficult question to answer, but here is my question
I hope this isn't too difficult, obviously you can call HTML tags such as
This is such a simple problem, but I can't find an answer anywhere... I
This seems like such a trivial problem, but I can't seem to pin how
this must be such a simple problem but can someone tell me why this
If I had some XML such as this loaded into an XDocument object: <Root>
This question will undoubtedly be difficult to answer and ask in a way that

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.