MethodBase.IsFinal Property

Definition

Gets a value indicating whether this method is final.

C#
public bool IsFinal { get; }

Property Value

true if this method is final; otherwise, false.

Implements

Examples

The following example displays false for IsFinal, which might lead you to think that MyMethod is overridable. The code prints false even though MyMethod is not marked virtual and thus cannot be overridden.

C#
using System;
using System.Reflection;

public class MyClass
{
    public void MyMethod()
    {
    }
    public static void Main()
    {
        MethodBase m = typeof(MyClass).GetMethod("MyMethod");
        Console.WriteLine("The IsFinal property value of MyMethod is {0}.", m.IsFinal);
        Console.WriteLine("The IsVirtual property value of MyMethod is {0}.", m.IsVirtual);
    }
}

Remarks

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 keyword in C#, NotOverridable keyword in Visual Basic, or sealed 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.

You can use this property, in conjunction with the IsVirtual property, to determine if a method is overridable. For a method to be overridable, IsVirtual property must be true and IsFinal property must be false. To establish with certainty whether a method is overridable, use code such as this:

C#
if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)

If IsVirtual is false or IsFinal is true, then the method cannot be overridden.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

See also