Export (0) Print
Expand All

MethodBase.IsFinal Property

Gets a value indicating whether this method is final.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

public bool IsFinal { get; }
/** @property */
public final boolean get_IsFinal ()

public final function get IsFinal () : boolean

Property Value

true if this method is final; otherwise, false.

To determine if a method is overridable, it is not sufficient to check that IsVirtual 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.

To establish with certainty whether a method is overridable, use code such as this:

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

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

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 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);
    }
}

import System.*;
import System.Reflection.*;

public class MyClass
{   
    public void MyMethod()
    {
    } //MyMethod

    public static void main(String[] args)
    {
        MethodBase m = MyClass.class.ToType().GetMethod("MyMethod");
        Console.WriteLine("The IsFinal property value of MyMethod is {0}.",
            System.Convert.ToString( m.get_IsFinal()));
        Console.WriteLine("The IsVirtual property value of MyMethod is {0}.",
            System.Convert.ToString( m.get_IsVirtual()));
    } //main
} //MyClass

import System;
import System.Reflection;
 
 public class MyClass 
 {
 public function MyMethod() : void
   {
   }
 public static function Main() : void 
   {
    var m : MethodBase  = MyClass.GetMethod("MyMethod");
    Console.WriteLine(m.IsFinal);
   }
 }
 MyClass.Main();

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft