Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

MethodBase::IsVirtual Property

 

Gets a value indicating whether the method is virtual.

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

public:
property bool IsVirtual {
	virtual bool get() sealed;
}

Property Value

Type: System::Boolean

true if this method is virtual; otherwise, false.

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

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:

No code example is currently available or this language may not be supported.

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

You can determine whether the current method overrides a method in a base class by calling the MethodInfo::GetBaseDefinition method. The following example implements an IsOverride method that does this.

No code example is currently available or this language may not be supported.

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2017 Microsoft