Join Nostr
2026-10-25 10:05:57 UTC

codementor on Nostr: Learn: Abstract class An abstract class is a base class you can't create directly. It ...

Learn: Abstract class

An abstract class is a base class you can't create directly. It can hold shared code and fields, plus abstract members each subclass must fill in.
A class can inherit from only one class.
Use it when: related classes share real code, not just a contract.

```csharp
Animal[] animals = { new Dog(), new Cat() };
foreach (var a in animals) Console.WriteLine(a.Describe());

abstract class Animal
{
public string Describe() => $"{GetType().Name} says {Sound()}"; // shared code
protected abstract string Sound(); // each subclass fills in
}
class Dog : Animal { protected override string Sound() => "woof"; }
class Cat : Animal { protected override string Sound() => "meow"; }
```

It prints Dog says woof, then Cat says meow.

Read more: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract

#tip · TIP-049