Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between Abstract method and Virtual method in C #

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the difference between Abstract method and Virtual method in C#". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Brief introduction:

Abstract and Virtual in C # are easy to confuse, both related to inheritance and involving the use of override. Virtual can be overridden by subclasses, while abstract must be overridden by subclasses. Virtual-decorated methods must be implemented (even if you just add a pair of curly braces), while abstract-decorated methods must not be implemented. They have one thing in common: if you use it to decorate a method, you must add public in front of it, or else you will have a compilation error: virtual or abstract methods cannot be private. After all, adding virtual or abstract allows subclasses to be redefined, and private members cannot be accessed by subclasses.

1. Virtual method (virtual method)

The virtual keyword is used to decorate methods in the base class. There are two ways to use virtual:

Case 1: the virtual method is defined in the base class, but the virtual method is not overridden in the derived class. Then in the call to the derived class instance, the virtual method uses the method defined by the base class.

Case 2: the virtual method is defined in the base class, and then overridden using override in the derived class. Then in the call to the derived class instance, the virtual method uses the derived overridden method.

Virtual can be overridden by subclasses:

Class BaseTest1 {public virtual void fun () {} / / must have an implementation} class DeriveTest1:BaseTest1 {/ / public override void fun () {}}

The method of virtual modification must be implemented (even if you just add a pair of curly braces):

Public class Test1 {public virtual void fun1 () {}} II. Abstract method (abstract method)

The abstract keyword can only be used to decorate methods in abstract classes, and there is no concrete implementation. The implementation of an abstract method must be implemented using the override keyword in a derived class.

The most essential difference between an interface and an abstract class: an abstract class is an incomplete class, an abstraction of an object, while an interface is a behavioral specification.

Abstract must be overridden by subclasses:

Abstract class BaseTest2 {public abstract void fun ();} class DeriveTest2: BaseTest2 {/ / public override void fun () Error 1: no implementation / / public void fun () {} error 2: no override / / override void fun () {} error 3: virtual or abstract members cannot be private (as long as virtual or abstract members are declared in the parent class, even if inherited) public override void fun () {} / / if the method is overridden Error: "A.DeriveTest2" does not implement inherited abstract member "A.BaseTest2.fun ()"}

The method of abstract modification must not be implemented:

Public abstract class Test2 {public abstract void fun2 ();} III. Keywords

Static: when a method is declared as Static, the method is static, and the compiler retains the implementation of the method at compile time. That is, this method belongs to a class, but does not belong to any member, regardless of whether an instance of the class exists or not. Just like the entry function Static void Main, because it is a static function, it can be called directly.

Virtua: when a method is declared as Virtual, it is a virtual method until you use ClassName variable = new ClassName (); it does not exist in real memory space until an instance of a class is declared. This keyword is not often used in class inheritance and is used to provide polymorphism support for class methods.

Overrride: indicates that overriding this class inherits from the Shape class

Virtual,abstract tells other classes that you want to inherit from him that you can override this method or property of mine, otherwise it is not allowed.

Abstract: an abstract method declaration is a method that must be overridden by a derived class, which is used to be inherited; it can be regarded as a virtual method without an implementation body; if the class contains an abstract method, then the class must be defined as an abstract class, whether or not it contains other general methods; an abstract class cannot have an entity.

This is the end of the content of "what is the difference between the Abstract method and the Virtual method in C#". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report