Permalink
Fetching contributors…
Cannot retrieve contributors at this time
72 lines (59 sloc) 3.57 KB
title ms.date ms.prod ms.technology ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author translation.priority.ht
override (C# Reference)
2015-07-20
.net
devlang-csharp
article
override
override_CSharpKeyword
CSharp
override keyword [C#]
dd1907a8-acf8-46d3-80b9-c2ca4febada8
26
BillWagner
wiwagn
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

override (C# Reference)

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

Example

In this example, the Square class must provide an overridden implementation of Area because Area is inherited from the abstract ShapesClass:

[!code-cscsrefKeywordsModifiers#1]

An override method provides a new implementation of a member that is inherited from a base class. The method that is overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method. For information about inheritance, see Inheritance.

You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.

An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.

You cannot use the new, static, or virtual modifiers to modify an override method.

An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property, and the overridden property must be virtual, abstract, or override.

For more information about how to use the override keyword, see Versioning with the Override and New Keywords and Knowing when to use Override and New Keywords.

Example

This example defines a base class named Employee, and a derived class named SalesEmployee. The SalesEmployee class includes an extra property, salesbonus, and overrides the method CalculatePay in order to take it into account.

[!code-cscsrefKeywordsModifiers#9]

C# Language Specification

[!INCLUDECSharplangspec]

See Also

C# Reference
C# Programming Guide
Inheritance
C# Keywords
Modifiers
abstract
virtual
new
Polymorphism