Share via


MethodBase.IsVirtual Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a value that indicates whether the method is virtual (Overridable in Visual Basic).

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

Syntax

'Declaration
Public ReadOnly Property IsVirtual As Boolean
public bool IsVirtual { get; }

Property Value

Type: System.Boolean
true if this method is virtual; otherwise, false.

Remarks

A virtual member may reference instance data in a class and must be referenced through an instance of the class.

To determine whether 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 that is non-virtual might implement an interface method. The common language runtime requires all methods that implement interface members to be marked as virtual; therefore, the compiler marks the method virtual final. Thus there are cases where a method that is marked as virtual is not overridable.

To establish with certainty whether a method is overridable, use C# code such as if (MethodInfo.IsVirtual && !MethodInfo.IsFinal) or Visual Basic code such as MethodInfo.IsVirtual And Not MethodInfo.IsFinal.

Examples

The following example displays false for IsFinal (NotOverridable in Visual Basic), which might lead you to think that MyMethod is overridable. MyMethod is not marked virtual (Overridable in Visual Basic) and therefore cannot be overridden.

Imports System.Reflection

Public Class Example

   Public Sub MyMethod()
   End Sub

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim m As MethodBase = GetType(Example).GetMethod("MyMethod")
      outputBlock.Text += String.Format("The IsFinal property value of MyMethod is {0}.", m.IsFinal) & vbCrLf
      outputBlock.Text += String.Format("The IsVirtual property value of MyMethod is {0}.", m.IsVirtual) & vbCrLf
   End Sub
End Class
using System;
using System.Reflection;

public class Example
{
   public void MyMethod()
   {
   }
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      MethodBase m = typeof(Example).GetMethod("MyMethod");
      outputBlock.Text += String.Format("The IsFinal property value of MyMethod is {0}.", m.IsFinal) + "\n";
      outputBlock.Text += String.Format("The IsVirtual property value of MyMethod is {0}.", m.IsVirtual) + "\n";
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.