Differences and usage of virtual, abstract, and override in C #

Source: Internet
Author: User
Virtual declares a method as a virtual method so that the derived class can override this method. Generally, the created method cannot be rewritten. For example, there is a method in Class.
Protected void method (){
Original code ....;
Class B is inherited from Class A. Class B can call the method () method of Class A, but cannot change the method body Code. However, when class A uses virtual declarations, this method is: protected virtual void method (), Class B can override this method by using override
Protected override void method (){
New Code ....;
Virtual can be used in base classes and abstract classes.
The abstract declaration method is an abstract method. The abstract method has no code body and only one method name is declared:
Protected abstract void method ();
When the abstract declaration method is used, its derived class must override this method. For example, Class B is declared in abstract class A and inherited from abstract class, override must be used to override this method in Class B.
Protected override void method (){
New Code ....;
Abstract can only be used in abstract classes.
Override is the keyword used by the derived class to override the parent class method (including the virtual method, abstract method, and interface method). If you want to override the method, you can use the override declaration.

Differences and usage of virtual, abstract, and override in C #

Related Article

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.