In fact, the difference between the two is to analyze the differences between the override and new keywords in the control class version. First, I will first list some official information related to this on the local MSDN 2008:
- "Override (C # reference )":( Ms-help: // MS. MSDNQTR. v90.chs/dv_csref/html/dd1907a8-acf8-46d3-80b9-c2ca4febada8.htm)
- "New modifier (C # reference )":( Ms-help: // MS. MSDNQTR. v90.chs/dv_csref/html/a2e20856-33b9-4620-b535-a60dbce8349b.htm)
- "Use the Override and New keywords for version control (C # programming guide )":( Ms-help: // MS. MSDNQTR. v90.chs/dv_csref/html/88247d07-bd0d-49e9-a619-45ccbbfdf0c5.htm)
- "Learn when to use Override and New keywords (C # programming guide )":( Ms-help: // MS. MSDNQTR. v90.chs/dv_csref/html/323db184-b136-46fc-8839-007886e7e8b0.htm)
Next, I will make a simple modification to an example in the above materials to better reflect the difference between the two in use. Using System;
Namespace KeywordsOfOverrideAndNew
{
// Define the base class
Class Car
{
Public virtual void DescribeCar ()
{
System. Console. WriteLine ("Call the DescribeCar () defined in Car class .");
}
}
// Define the derived classes
Class ConvertibleCar: Car
{
// Use new
Public new virtual void DescribeCar ()
{
System. Console. WriteLine ("Call the DescribeCar () defined in ConvertibleCar class .");
}
}
Class Minivan: Car
{
// Use override
Public override void DescribeCar ()
{
System. Console. WriteLine ("Call the DescribeCar () defined in Minivan class .");
}
}
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("Declare object type by each classes :");
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 ("----------");
& N