Home  >  Article  >  Backend Development  >  What is the difference between virtual functions and abstract functions in C#?

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

WBOY
WBOYforward
2023-08-29 10:37:05724browse

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() { }

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();
      }
   }
}

Output

Rectangle class area
Area: 70
Triangle class area:
Area: 25

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();
   }
}

Output

Bus
Car
Motorcycle

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete