How to verify that an object implements an interface

Given this scenario

interface A {}

class B : A {}

A b = new B();

How can I verify that object b is created from interface A?

+5
source share
4 answers

Try using

if(b is A)
{
    // do something
}

- Is this what you want?

+10
source

You can test it like this:

var b = new B();

var asInterface = x as A;
if (asInterface == null) 
{
    //not of the interface A!
}
+5
source
+3

:

IMyInterface = instance as IMyInterface;
if (intance != null)
{
//do stuff
}

'as' - , 'is', - Impelments IMyInterface, .

0

All Articles