Skip to content

Commit 0fd32ac

Browse files
Improve MethodBase.IsFinal remarks (#3723)
- Reword description to specify more clearly what "final" means and how it maps into language keywords. - Add Visual Basic code snippet
1 parent 130bcf6 commit 0fd32ac

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

xml/System.Reflection/MethodBase.xml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,14 +1599,21 @@
15991599
<remarks>
16001600
<format type="text/markdown"><![CDATA[
16011601
1602-
## Remarks
1603-
To determine if a method is overridable, it is not sufficient to check that <xref:System.Reflection.MethodBase.IsVirtual%2A> is `true`. For a method to be overridable, `IsVirtual` must be `true` and `IsFinal` must be `false`. For example, a method might be non-virtual, but it implements an interface method. The common language runtime requires that all methods that implement interface members must be marked as `virtual`; therefore, the compiler marks the method `virtual final`. So there are cases where a method is marked as `virtual` but is still not overridable.
1604-
1605-
To establish with certainty whether a method is overridable, use code such as this:
1606-
1607-
`if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)`
1608-
1609-
If `IsVirtual` is `false` or `IsFinal` is `true`, then the method cannot be overridden.
1602+
## Remarks
1603+
1604+
If the virtual method is marked `final`, it can't be overridden in derived classes. The overridden virtual method can be marked `final` using the [sealed](~/docs/csharp/language-reference/keywords/sealed.md) keyword in C#, [NotOverridable](~/docs/visual-basic/language-reference/modifiers/notoverridable.md) keyword in Visual Basic, or [sealed](/cpp/extensions/sealed-cpp-component-extensions) keyword in C++/CLI. The method can also be marked `final` implicitly by the compiler. For example, a method might be defined as non-virtual in your code, but it implements an interface method. The Common Language Runtime requires that all methods that implement interface members must be marked as `virtual`; therefore, the compiler marks the method `virtual final`.
1605+
1606+
You can use this property, in conjunction with the <xref:System.Reflection.MethodBase.IsVirtual%2A> property, to determine if a method is overridable. For a method to be overridable, <xref:System.Reflection.MethodBase.IsVirtual%2A> property must be `true` and `IsFinal` property must be `false`. To establish with certainty whether a method is overridable, use code such as this:
1607+
1608+
```csharp
1609+
if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)
1610+
```
1611+
1612+
```vb
1613+
If MethodInfo.IsVirtual AndAlso Not MethodInfo.IsFinal Then
1614+
```
1615+
1616+
If `IsVirtual` is `false` or `IsFinal` is `true`, then the method cannot be overridden.
16101617
16111618
16121619

0 commit comments

Comments
 (0)