Casting Deriving Class as Base Class

Suppose I have this:

using System;

public class Program
{
    public static void Main()
    {
        BaseClass bc = new DerivedClass();
        bc.Method1();
        bc.Method2();
        Console.WriteLine(bc.GetType().FullName);

        // Output
        // Derived - Method1 (override)
       // Base - Method2
       // DerivedClass
    }
}

public class BaseClass
{
    public virtual void Method1()
    {
        Console.WriteLine("Base - Method1");
    }

    public virtual void Method2()
    {
        Console.WriteLine("Base - Method2");
    }
}

public class DerivedClass : BaseClass
{
    public override void Method1()
    {
        Console.WriteLine("Derived - Method1 (override)");
    }

    public new void Method2()
    {
        Console.WriteLine("Derived - Method2 (new)");
    }
}

If the instance variable of the deriving class is cast to the base class and that instance variable is used to call the overridden methods, the method overridden with the override keyword will execute the implementation in the deriving class whereas the one overridden with the new keyword will execute the implementation in the base class.

How is the variable bc in the above example cast to the Base Class?

I know that the new keyword will override the method implementation in the deriving class and it will be executed when an instance variable of the deriving class is used to call the overridden method, but I don't know what type of Conversion it is.. Doesn't seem to be Implicit nor Explicit, may be Type Conversion but I am confused by the syntax.

Any explanation is appreciated.


I know that the new keyword will override the method implementation in the deriving class

No. It does not override the base class's method. It declares a new, independent method, that's just named the same and has the same signature. The effect is it hide the same-signature method declared in the base class, effectively complicating the invocation of the same-signature method declared in the base class.

In your example there are no 'type conversions' whatsoever. Think of a type cast as providing a specific view of the instance—exposing the specific part of the class's contract to the user. It's nothing more, nothing less.

Example:

// instance of DerivedClass exposing its full contract via the 'dc' variable
DerivedClass dc = new DerivedClass();

// the same instance of DerivedClass exposing its contract limited to what's declared in BaseClass
BaseClass bc = dc;

// calling Method2 as newly declared in DerivedClass
dc.Method2();

// calling Method2 as declared in BaseClass—the following two lines are equivalent
bc.Method2();
((BaseClass)dc).Method2();

How is the variable bc in the above example cast to the Base Class?

It is an implicit cast which is performed when you assign your new DerivedClass instance to a variable of the BaseClass type:

BaseClass bc = new DerivedClass();

Practically speaking, there is no conversion. There is only the way you look at the object.

  • If you look at it with base-class-glasses, you will see all base-class-methods, which includes the overridden Method1 from the derived class, but it will not include the new Method2 from the derived class, so you will only see Method2 from the base class.
  • If you look at it with derived-class-glasses, you will see all base-class-methods, which includes the overridden Method1 from the derived class, but it will now also include the new Method2 from the derived class. The original virtual Method1 from the base class, will not be visible.
  • 链接地址: http://www.djcxy.com/p/39444.html

    上一篇: 部署后升级SQL Express数据库的工具

    下一篇: 将派生类作为基类