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 #