What is the difference between virtual functions and abstract functions in C#?

WBOY
Release: 2023-08-29 10:37:05
forward
711 people have browsed it

C# 中的虚函数和抽象函数有什么区别?

Abstract methods do not provide implementation, they force derived classes to override the method. It is declared under abstract class. Abstract methods only have method definitions

Virtual methods have an implementation, and unlike abstract methods, they can exist in both abstract and non-abstract classes. It gives derived classes the option to override it.

Virtual functions

The virtual keyword is useful when modifying methods, properties, indexers, or events. You can use virtual functions when you have defined a function in a class and want to implement that function in inherited classes. Virtual functions can have different implementations in different inherited classes, and calls to these functions will be determined at runtime.

The following is a virtual function-

public virtual int area() { }
Copy after login

The following example shows how to use a virtual function-

Example

Real-time demonstration

using System; namespace PolymorphismApplication { class Shape { protected int width, height; public Shape( int a = 0, int b = 0) { width = a; height = b; } public virtual int area() { Console.WriteLine("Parent class area :"); return 0; } } class Rectangle: Shape { public Rectangle( int a = 0, int b = 0): base(a, b) { } public override int area () { Console.WriteLine("Rectangle class area "); return (width * height); } } class Triangle: Shape { public Triangle(int a = 0, int b = 0): base(a, b) { } public override int area() { Console.WriteLine("Triangle class area:"); return (width * height / 2); } } class Caller { public void CallArea(Shape sh) { int a; a = sh.area(); Console.WriteLine("Area: {0}", a); } } class Tester { static void Main(string[] args) { Caller c = new Caller(); Rectangle r = new Rectangle(10, 7); Triangle t = new Triangle(10, 5); c.CallArea(r); c.CallArea(t); Console.ReadKey(); } } }
Copy after login

Output

Rectangle class area Area: 70 Triangle class area: Area: 25
Copy after login

Abstract function

The abstract keyword in C# is used for abstract classes and abstract functions. Abstract classes in C# include abstract methods and non-abstract methods.

The following is an example of an abstract function in an abstract class in C# -

Example

Live demonstration

using System; public abstract class Vehicle { public abstract void display(); } public class Bus : Vehicle { public override void display() { Console.WriteLine("Bus"); } } public class Car : Vehicle { public override void display() { Console.WriteLine("Car"); } } public class Motorcycle : Vehicle { public override void display() { Console.WriteLine("Motorcycle"); } } public class MyClass { public static void Main() { Vehicle v; v = new Bus(); v.display(); v = new Car(); v.display(); v = new Motorcycle(); v.display(); } }
Copy after login

Output

Bus Car Motorcycle
Copy after login

The above is the detailed content of What is the difference between virtual functions and abstract functions in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!