About
In this code snippet, we’ll find out what the abstract keyword does in C#.
Classes and their members such as methods, properties, … that are marked with abstract keyword aren’t fully implemented. Abstract classes aren’t meant to be instantiated but are only meant to be inherited from. This means that class members(methods, properties, …) marked with the abstract keyword must be implemented in the child class.
Let’s look at the example below.
Code:
using System; namespace AbstractClass { class Program { static void Main(string[] args) { MyChildClass MCC = new MyChildClass(); MCC.myAbstractMethod(); Console.ReadLine(); } } //This abstract class can't be instantiated. It can only be used to inherit from. //Abstract members will have to be implemented in the derived class. abstract class MyClass { public abstract void myAbstractMethod(); } class MyChildClass : MyClass { //Implementation of the myAbstractMethod() method. public override void myAbstractMethod() { Console.WriteLine("From myAbstractMethod."); } } }