abcprep.com - your onestop GMAT, GRE, PMP hub
.NET FAQs Unleashed!
 
    



    


How to figure out in C#, at runtime, whether an object of a class, implements an interface or not?

If a class implements an interface, there must be a way to find out at run time, whether the object of this class implements an interface or not. There are three ways to do this:

1) Explicit cast - A try catch block may be weaved in the C# code, to figure out whether a class supports an interface or not. This may be done by explicitly casting an object to the interface itself. See code below to understand how this works:

//Say we have an interface by the name IBooks

//Let there be a class by the name MathsBooks, which may or may not implement IBooks

//Lets find out using a try catch block, how we may figure this out...

MathsBooks mb = new MathsBooks();

IBooks ib;

try

{

   ib = (IBooks)mb; //Here, we are explicitly casting the object mb to IBooks

   Messagebox.Show("It works, MathsBooks does implement from IBooks");

}

catch(InvalidCastException ex)

{

  Messagebox.Show(ex.Message);

  Messagebox.Show("It doesn't work, MathsBooks does not implement from IBooks");

}

2) Using the "as" keyword - getting a reference to an interface

Using the "as" keyword to check whether an object supports an interface or not, is pretty simple. For this, an instance of the interface needs to be set to the class's object, which is internally casted to the interface using the "is" keyword. See code example below for more clarity:

MathsBooks mb = new MathsBooks();

IBooks ib = mb as IBooks;

if(ib == null)

{

  Messagebox.Show("The class MathsBooks does not implement the interface IBooks");

}

else

{ Messagebox.Show("The class MathsBooks does implement the interface IBooks");}

2) Using the "is" keyword - getting a reference to an interface

This is perhaps, the simplest way to check for an interface reference. If an interface is being implemented by an object of a class, the "is" keyword may be used. This may be checked in combination with an if statement. See code example below:

MathsBooks mb = MathsBooks();

if(mb is IBooks)

{

   Messagebox.Show("The class MathsBooks does implement the interface IBooks");}

}

 

 

 

Protected Internal  Array  Sort Array  Throw  Multiple Catch  Polymorphism  Inheritance  Session Viewstate  Autogenerate  ReadOnly  Sealed Class  Sealed Method  Multiple Interfaces  Overloading  Overloaded Constructors  Generics  Main  Case Sensitive  Console  Specifier  Return Type  SetCommandLineArgs  System Environment  New  Default Values of Types Compiler Error Constant Variable Const Const - ReadOnly Parameter Modifier Out Ref Interface Interface Reference, is, as System.Collection

More Interview Questions...
Skip Navigation Links
  Design Patterns  
  Ajax
  Security
  C#
  ASP.NET
 ASP.NET 2.0
 Articles
SQL
Skip Navigation Links
  OOPs 
  Framework
  ADO.NET
  100 Short Q-As
  Difference Between Q-As
  .NET Acronyms
  Discussions








Know Your Breath, Know Your Life!!!