Conversions of Classes (Reference Types) To the Base Class of an Object : cast « Class « C# / CSharp Tutorial






using System;

public class Base
{
    public virtual void talk()
    {
        Console.WriteLine("Base");
    }
}
public class Derived: Base
{
    public override void talk()
    {
        Console.WriteLine("Derived");
    }
}
public class Test
{
    public static void Main()
    {
        Derived d = new Derived();
        Base b = d;
        
        b.talk();
        Derived d2 = (Derived) b;
        
        object o = d;
        Derived d3 = (Derived) o;
    }
}
Derived








7.30.cast
7.30.1.Conversions of Classes (Reference Types) to an Interface the Object Might Implement
7.30.2.Conversions of Classes (Reference Types) To the Base Class of an Object