ZetCode

C# override modifier

last modified October 18, 2026

In this article we show how to use the override modifier in C#.

In C#, the override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event. Only members marked virtual, abstract, or override in the base class can be overridden. The overriding member must have the same signature and the same accessibility.

C# override virtual method

The Object is the root of the C# type system. It supports all classes and provides low-level services to derived classes.

The Object.ToString returns a string that represents the current object. The default implementation of the method returns the fully qualified name of the object's type.

public virtual string? ToString();

The ToString method has a virtual modifier.

It is common practice to override the Object.ToString method to provide human-readable representation of our custom objects.

Program.cs
var u = new User("John Doe", "gardener");
Console.WriteLine(u);

class User(string name, string occupation)
{
    public string Name { get; set; } = name;
    public string Occupation { get; set; } = occupation;

    public override string ToString()
    {
        return $"{Name} is a {Occupation}";
    }
}

In the program, we create our own implementation of the Object.ToString method.

var u = new User("John Doe", "gardener");
Console.WriteLine(u);

The ToString method is called when we pass the user instance to the Console.WriteLine method.

public override string ToString()
{
    return $"{Name} is a {Occupation}";
}

We use the override keyword to override the default implementation of the method, which is declared to be virtual.

$ dotnet run
John Doe is a gardener

C# override abstract method

Abstract classes provide a common definition for descendant classes. An abstract method has no implementation; it must be overridden in every non-abstract derived class.

Program.cs
var c = new Circle(12, 45, 22);

Console.WriteLine(c);
Console.WriteLine($"Area of circle: {c.Area():F2}");
Console.WriteLine(c.GetCoordinates());

abstract class Drawing
{
    protected int x = 0;
    protected int y = 0;

    public abstract double Area();

    public string GetCoordinates()
    {
        return $"x: {x}, y: {y}";
    }
}

class Circle : Drawing
{
    private int r;

    public Circle(int x, int y, int r)
    {
        this.x = x;
        this.y = y;
        this.r = r;
    }

    public override double Area()
    {
        return r * r * Math.PI;
    }

    public override string ToString()
    {
        return $"Circle at x: {x}, y: {y}, radius: {r}";
    }
}

We have an abstract base Drawing class. The class defines two member fields, defines one method, and declares one abstract method.

public abstract double Area();

The Area method is abstract and thus, it must be overridden in the concrete child class.

public override double Area()
{
    return r * r * Math.PI;
}

Inside the Circle class, we override the Area method and provide its implementation.

$ dotnet run
Circle at x: 12, y: 45, radius: 22
Area of circle: 1520.53
x: 12, y: 45

C# override with base and sealed

An overriding member can call the implementation of the base class with the base keyword. With the sealed modifier, a derived class can prevent further overriding of the member. Properties can be overridden in the same way as methods.

Program.cs
Animal[] animals = [new Animal(), new Dog(), new Puppy()];

foreach (var animal in animals)
{
    Console.WriteLine($"{animal.Kind}: {animal.Describe()}");
}

class Animal
{
    public virtual string Kind => "animal";

    public virtual string Describe() => "breathes";
}

class Dog : Animal
{
    public override string Kind => "dog";

    public sealed override string Describe() => base.Describe() + ", barks";
}

class Puppy : Dog
{
    public override string Kind => "puppy";

    // public override string Describe() => "plays"; error CS0239: sealed member
}

We have a hierarchy of three classes.

public override string Kind => "dog";

The virtual Kind property is overridden in each class.

public sealed override string Describe() => base.Describe() + ", barks";

The Dog class extends the behaviour of the base method instead of replacing it: it calls base.Describe and adds to the result. The method is sealed, so the Puppy class cannot override it; the commented line would cause the CS0239 error.

$ dotnet run
animal: breathes
dog: breathes, barks
puppy: breathes, barks

The puppy inherits the sealed Describe method of the dog, but it provides its own Kind.

C# override vs new

With the new keyword we can explicitly hide a member that is inherited. When we hide an inherited member, the derived version of the member replaces the base class version only when we access the object through the derived type. Through a reference of the base type, the original member is still called.

While the override modifier replaces the implementation of the original method (there is one method), the new modifier creates a new method that hides the original one (there are two methods).

Program.cs
Base[] objs = { new Base(), new Derived(), new Base() };

Console.WriteLine("-------------------------");
Console.WriteLine("Info/override");

Derived d = new Derived();
d.Info();
((Base)d).Info();

Base b = new Base();
b.Info();

Console.WriteLine("------------");

foreach (Base obj in objs)
{
    obj.Info();
}

Console.WriteLine("-------------------------");
Console.WriteLine("Info2/new");

Derived d2 = new Derived();
d2.Info2();
((Base)d2).Info2();

Base b2 = new Base();
b2.Info2();

Console.WriteLine("------------");

foreach (Base obj in objs)
{
    obj.Info2();
}

class Base
{
    public virtual void Info()
    {
        Console.WriteLine("Base class");
    }

    public virtual void Info2()
    {
        Console.WriteLine("Base class");
    }
}

class Derived : Base
{
    public override void Info()
    {
        Console.WriteLine("Derived class");
    }

    public new void Info2()
    {
        Console.WriteLine("Derived class");
    }
}

We have a Base class and a Derived class. There are two methods: Info and Info2. The first uses the override keyword in the Derived, the second uses the new keyword.

$ dotnet run
-------------------------
Info/override
Derived class
Derived class
Base class
------------
Base class
Derived class
Base class
-------------------------
Info2/new
Derived class
Base class
Base class
------------
Base class
Base class
Base class

The overridden Info method prints "Derived class" for a derived object even when it is accessed through the Base type. The hiding Info2 method is used only when the variable has the Derived type.

Source

override modifier - language reference

In this article we covered the C# override modifier.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all C# tutorials.