Understanding of virtual, override, and new keywords in C #

Source: Internet
Author: User

Source: http://blog.csdn.net/jackiezhw/article/details/2673992

 

In C #, a derived class can contain methods with the same name as a base class method.

  • The base class method must be defined as virtual.
  • If the method in the derived class does not have the new or override keyword before it, the compiler will issue a warning that the method will execute the operation like there is a new keyword.
  • If the method in the derived class has a new keyword before it, the method is defined as a method independent from the method in the base class.
  • If the method in the derived class has the override keyword before it, the object of the derived class will call this method instead of the base class method.
  • You can use the base keyword in the derived class to call the base class method.
  • The override, virtual, and new keywords can also be used in attributes, indexers, and events.
 1 using System;   2 using System.Collections.Generic;   3    4    5 public class Car   6 {   7    8     public virtual void DescribeCar()   9     {  10         Console.WriteLine("Base Car!");  11     }  12   13 }  14   15   16 public class ConvertibleCar : Car  17 {  18     public new void DescribeCar()  19     {  20         base.DescribeCar();  21         Console.WriteLine("Convertible Car!");  22     }  23 }  24   25 public class DerivedConvertibleCar : ConvertibleCar  26 {  27     public new void DescribeCar()  28     {  29         base.DescribeCar();  30         Console.WriteLine("DerivedConvertibleCar Car!");  31     }  32 }  33   34   35 public class DerivedMinivan : Minivan  36 {  37     public override void DescribeCar()  38     {  39         base.DescribeCar();  40         Console.WriteLine("DerivedMinivan Car!");  41     }  42 }  43 public class Minivan : Car  44 {  45     public override void DescribeCar()  46     {  47         base.DescribeCar();  48         Console.WriteLine("Minivan Car!");  49     }  50 }  51   52   53 public class CarCollections  54 {  55     public static void Main()  56     {  57         List<CAR> cars = new List<CAR>();  58   59         cars.Add(new Car());  60         cars.Add(new ConvertibleCar());  61         cars.Add(new Minivan());  62         cars.Add(new DerivedConvertibleCar());  63         cars.Add(new DerivedMinivan());  64         foreach (Car car in cars)  65             car.DescribeCar();  66     }  67 }  


Output result:

Base Car!【Car】Base Car!【ConvertibleCar】Base Car!【Minivan】Minivan Car!Base Car!【DerivedConvertibleCar】Base Car!【DerivedMinivan 】Minivan Car!DerivedMinivan Car!

 

We can see that the effects of using new and override are different.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.