C# interfaces

  • Thread starter Thread starter William F. Robertson, Jr.
  • Start date Start date
W

William F. Robertson, Jr.

This is probably a more C# language question, but how can I tell if an
object implements a certain interface?

bill
 
On Wed, 2 Jul 2003 10:49:48 -0500, William F. Robertson, Jr.


Since you mentioned C#, I'll give my answer in C#.


if (o is IMyInterface) {
// object o implements IMyInterface
}
else {
// object o doesn't implement IMyInterface
}

-chris
 
Hello,

there is also the 'as' keyword:

IMyInterface i = o as IMyInterface
if(i != null)
{
// i implements IMyInterface
}
else
{
// i does not implements IMyInterface
}

it allow you to bypass the cast that you need to put when using 'is' keyword

Now my question: Is there any fundamental diference between these 2 ways to
do slighty the same thing?

Gauthier
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

News Neuralink Brain-Machine Interface 4
Interface that's not an interface? 2
interfaces in c#.net 1
GridView bug? 2
dynamic casting 2
Independent interface implementation 3
Seesion Manager 1
Implement com interface 1

Back
Top