简体   繁体   中英

C# Class Type conversion

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();
        }
    }
}

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. ie 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.

eg:

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, ie 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, eg

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.

Just because you store it in a variable of Type A, your variable still references an object of Type B. When you make calls on your variable "a", the CLR knows the correct method to call. Since you overrode the show method, the show method of type B will be called. But you didn't override test(), so the method implementation on type A is called. This is possible because B "inherited" the test() behavior from A, but didn't override it.

In your code:

 // outputs showing B
  a.show();

prints "Showing B" because show() is a virtual method. Any virtual method call is determined by the object type, not the type of the reference (late binding). The reference a is pointing to an object of type B .

  // outputs called from A
  a.test();

test() is simply inherited from class A and not a virtual method, so it prints "called from A". The method call a.GetType() doesn't convert anything, it simply returns a instance of a Type-object.

Variable a points to an instance of type B , so no conversion is required in order for a.show() to the B.show() method. That's precisely what virtual methods are supposed to do: call the method on the instantiated type (or the first implementation in the inheritance chain) that the variable references, not the declared type of the variable.

The call to a.test() won't do that because the method isn't declared virtual . So it simply calls the A.test() method.

As mentioned in the other posts you have created an instance of type B but because you up-casted the object to type A you have the interfaces defined for type A.

If need to access to methods defined for type B you can down-cast the object to it's original type which gives you the definition for type B.

for instance:

if (a is B)
{
    ((B)a).testb();
}

// or

B b = a as B;
if (b != null)
{
    b.testb();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM