Uređivanje

Dijeli putem


MethodBase.IsFinal Property

Definition

Gets a value indicating whether this method is final.

public:
 property bool IsFinal { bool get(); };
public bool IsFinal { get; }
member this.IsFinal : bool
Public ReadOnly Property IsFinal As Boolean

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.

using namespace System;
using namespace System::Reflection;

public ref class MyClass
{
public:
   void MyMethod(){}
};

int main()
{
   MethodBase^ m = MyClass::typeid->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 );
}
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);
    }
}
Imports System.Reflection

Public Class MyClass1
    
    Public Sub MyMethod()
    End Sub
    
    Public Shared Sub Main()
        Dim m As MethodBase = GetType(MyClass1).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)
    End Sub
End Class

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:

if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)
If MethodInfo.IsVirtual AndAlso Not MethodInfo.IsFinal Then

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

Applies to

See also