The difference between abstract methods and virtual methods in C #

Source: Internet
Author: User

People who have studied C # should know both abstract and virtual methods, and many beginners don't know much about the difference between them. Today this article is to analyze the difference between the two. And an example is attached to illustrate. The specific analysis is as follows:


First, the main difference:


For abstract methods, subclasses must implement it


For virtual methods, subclasses can override or not override

So

The two constraints are different


Second, the example code is as follows:

/* Declare an abstract class * 1. The abstract class can contain variables * 2. Method body */abstract Class absclass{string name cannot be declared in abstract method  ;  public abstract void Displayvalue (string value);} /* Subclass inheriting abstract class must implement abstract method */class absclassinherited:absclass{/  * Use override override to implement */public  override void Displayvalue (String value)  {    Console.WriteLine (value. ToUpper ());}  } /* Declare virtual function */class virtclass{  /* Declares a virtual function   virtual function must implement method body *  /public virtual void Displayvalue (string value)  {    Console.WriteLine (value);  }} /* Virtual method can be implemented or not implemented */class virtclassinherited:virtclass{/  * Use override override to implement */public  override void Displayvalue (string value)  {    Console.WriteLine (value. ToUpper ());}  } /* Declare an interface * 1. The methods in the interface must be public * 2. The variable is not allowed in the interface * 3. Methods in the interface do not allow method body */interface iabs{  void Displayvalue (string value);}

I hope the analysis of this article will be helpful to everyone's C # program design.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
The difference between abstract methods and virtual methods in C #

This address: http://www.paobuke.com/develop/c-develop/pbk23625.html






Related content C # using MCI to create a video or sound player source code download C # Implement a method to close a child window without releasing a child window object C # Operations a basic tutorial for database Modeling of LINQ to SQL Components C # Custom controls how to add a context menu
Implementation of the XML file based on C # tool class in C # Dictionary class using instances C # Methods of implementing queues through a linked list WinForm traps for relative paths

The difference between abstract methods and virtual methods 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.