Syntax
class Child : Parent {\n public Child() : base() { }\n}Examples
Basic Inheritance
A derived class automatically gains all accessible members of its base class.
public class Animal
{
public string Name;
public Animal(string name)
{
Name = name;
}
public void Speak()
{
Console.WriteLine($"{Name} makes a sound");
}
}
public class Dog : Animal
{
public Dog(string name) : base(name) // call the base constructor
{
}
}
Dog rex = new Dog("Rex");
rex.Speak(); // Rex makes a sound (inherited method)Using base to Extend a Constructor
Calling the parent's constructor while adding subclass-specific initialization.
public class Animal
{
public string Name;
public Animal(string name) { Name = name; }
}
public class Cat : Animal
{
public bool IsIndoor;
public Cat(string name, bool indoor) : base(name)
{
IsIndoor = indoor;
}
}
Cat whiskers = new Cat("Whiskers", true);
Console.WriteLine($"{whiskers.Name}, indoor: {whiskers.IsIndoor}");Method Overriding with virtual and override
A base class marks a method virtual to allow overriding; the derived class uses override to replace it.
public class Animal
{
public virtual void Speak()
{
Console.WriteLine("Some generic animal sound");
}
}
public class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Woof!");
}
}
Animal a = new Dog();
a.Speak(); // Woof! - the overridden version runsCalling the Base Version with base
A subclass can extend, rather than fully replace, the parent's behavior by calling base.Method().
public class Employee
{
public virtual void Work()
{
Console.WriteLine("Doing general work tasks");
}
}
public class Manager : Employee
{
public override void Work()
{
base.Work(); // still do the general work first
Console.WriteLine("Also managing the team");
}
}
Manager m = new Manager();
m.Work();
// Doing general work tasks
// Also managing the teamBest practices
- Use : base(...) in a derived class constructor to reuse the parent's setup logic instead of duplicating it
- Mark a base class method virtual only when you actually intend subclasses to override it - not every method needs to be overridable
- Use base.Method() inside an override when you want to extend the parent behavior rather than fully replace it
- Favor composition (a class containing an instance of another) over deep inheritance chains when the relationship is not truly "is-a"
At a glance
- Purpose
- Applications on the .NET platform
- File extension
- .cs
- Runs in
- .NET runtime
- Usually used with
- .NET SDK and libraries
Specifications & further reading
Related C# documentation
A class is a blueprint for creating objects, bundling related data (fields/properties) and behavior (methods) together. C# is a fully object-oriented language - every application has at least one class, typically with a Main method as its entry point. An object is a specific instance of a class, created with the new keyword, with its own independent copy of the class's instance data.Constructors
A constructor is a special method that runs automatically when an object is created with new, used to initialize the object's state. A constructor shares its name with the class and has no return type. C# supports constructor overloading (multiple constructors with different parameter lists) and constructor chaining with this(...), where one constructor calls another in the same class to avoid duplicating initialization logic.Properties (get/set)
Properties are C#'s idiomatic way to expose class data through accessor-like syntax while still allowing controlled access via get and set. Unlike a plain public field, a property can validate a value before it's set, compute a value on the fly, or restrict access to read-only. Auto-implemented properties (using { get; set; } with no body) provide a concise shorthand when no custom logic is needed.Access Modifiers
Access modifiers control the visibility of classes, fields, methods, and properties from other parts of a program. C# provides public (accessible from anywhere), private (accessible only within the declaring class - the default for class members), protected (accessible within the class and its subclasses), internal (accessible only within the same assembly/project), and combinations like protected internal and private protected.