How to check object instances are of a specific class or inherited from a specific class
C#, C# Language February 22nd, 2006You can determine if an object is of a specific class by using the typeof keyword.
[csharp]
if (myObj.GetType() == typeof(MyClass))
{
// Do something
}[/csharp]
If you want to check to see if an object is of a specific type, or can be cast to a specific type then use the is operator.
[csharp]
if (myObj is MyClass)
{
// Do something
}
[/csharp]
Recent Comments