.

virtual & override vs new

using System;
using System.Collections.Generic;

public class MyClass
{
    public static void RunSnippet()
    {
TestCars2();
    }
   
   
    public static void TestCars1()
{
    Car car1 = new Car();
    car1.DescribeCar();
    System.Console.WriteLine(“———-”);

    ConvertibleCar car2 = new ConvertibleCar();
    car2.DescribeCar();
    System.Console.WriteLine(“———-”);

    Minivan car3 = new Minivan();
    car3.DescribeCar();
    System.Console.WriteLine(“———-”);
}

public static void TestCars2()
{
    Car[] cars = new Car[3];
    cars[0] = new Car();
    cars[1] = new ConvertibleCar();
    cars[2] = new Minivan();
   
    foreach (Car vehicle in cars)
{
    System.Console.WriteLine(“Car object: ” + vehicle.GetType());
    vehicle.DescribeCar();
    System.Console.WriteLine(“———-”);
}
}
    // Define the base class
class Car
{
    public virtual void DescribeCar()
    {
        System.Console.WriteLine(“base class: Car ‘s description”);
    }
}

// Define the derived classes
class ConvertibleCar : Car
{
    //this doesnt override (car)ConvertibleCar calling as car.describe() but it overrides (ConvertibleCar)ConvertibleCar.DescribeCar
    //so it leaves the base class function in place as (car)ConvertibleCar.DescribeCar
    //only override on ConvertibleCar class level – leave base in tact
    public new virtual void DescribeCar()
    {       
        System.Console.WriteLine(“ConvertibleCar : Car … public new virtual void DescribeCar().”);
        System.Console.WriteLine(“calling base.DescribeCar();”);
        System.Console.Write(“……”);
        base.DescribeCar();
    }
}

class Minivan : Car
{
    // this overrides (car)minivan calling as car.describe() with the following .. and base.descr() is still available
        //so it leaves the old base class function availabe as base.DescribeCar
        //overrides ConvertibleCar base class level : Minivan
    public override void DescribeCar()
    {
        System.Console.WriteLine(“Minivan : Car … public override void DescribeCar().”);
        System.Console.WriteLine(“calling base.DescribeCar();”);
        System.Console.Write(“……”);
        base.DescribeCar();
    }
}
   
   
    #region Helper methods
   
    public static void Main()
    {
        try
        {
            RunSnippet();
        }
        catch (Exception e)
        {
            string error = string.Format(“—\nThe following error occurred while executing the snippet:\n{0}\n—”, e.ToString());
            Console.WriteLine(error);
        }
        finally
        {
            Console.Write(“Press any key to continue…”);
            Console.ReadKey();
        }
    }

    private static void WL(object text, params object[] args)
    {
        Console.WriteLine(text.ToString(), args);   
    }
   
    private static void RL()
    {
        Console.ReadLine();   
    }
   
    private static void Break()
    {
        System.Diagnostics.Debugger.Break();
    }

    #endregion
   
    /*public class A
{
   public virtual void One();
   public void Two();
}

public class B : A
{
   public override void One();
   public new void Two();
}

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

a.One(); // Calls implementation in B
a.Two(); // Calls implementation in A
b.One(); // Calls implementation in B
b.Two(); // Calls implementation in B*/
}

What's your thoughts on this?

*

Protected by WP Anti Spam