Check Class implemented Interface

 

As we know class can implement one interface or multiple interfaces. Sometimes we might want to know what are all interfaces implemented by particular class. Today we discuss about how to check whether class implemented particular interface or not.

 

First create on interface ICompany which has two methods GetNumberOfEmp() and GetNumberOfBranches() as shown below.

 

interface ICompany

{

        int GetNumberOfEmp();

        int GetNumberOfBranches();

}

 

Create two classes COmpanyA and CompanyB as shown below.

 

class CompanyA : ICompany

{

        #region ICompany Members

 

        public int GetNumberOfEmp()

        {

            return 1000;

        }

 

        public int GetNumberOfBranches()

        {

            return 10;

        }

 

        #endregion

}

 

class CompanyB

{

        string GetCompanyInfo()

        {

            return "This is from CompanyB";

        }

}

 

As shown above CompanyA implemented interface ICompany whereas CompanyB does not implemented. Now validate CompanyA & CompanyB classes to check whether these classes implemented interface ICompany or not programmatically as shown below.

 

private void Form1_Load(object sender, EventArgs e)

{

            CompanyA objA = new CompanyA();

            CompanyB objB = new CompanyB();

 

            ICompany iCompany = null;

 

            try

            {

                iCompany = (ICompany)objA;

                MessageBox.Show("CompanyA implemented interface ICompany", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

                MessageBox.Show("CompanyA not implemented interface ICompany","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

            }

 

            try

            {

                iCompany = (ICompany)objB;

                MessageBox.Show("CompanyB implemented interface ICompany", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                MessageBox.Show("CompanyB not implemented interface ICompany", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

}

 

As shown above we created the objects for both classes CompanyA, CompanyB as objA & objB. Create reference iCompany for interface ICompany. Then try to assign the objA, objB objects to reference variable iCompany by casting with interface ICompany. If you run the application the output is as shown below.

 

                                      

                      

 

                                   

 

CompanyA object objA is successfully assigned to ICompany reference variable iCompany because Company implemented interface ICOmpany whereas while converting CompanyB object to ICompany interface we will gget type casting error because CompanyB not implemented ICompany interface so it won’t support ICompany interface type.

 

In this way we can find whether class implemented particular interface or not by casting with that interface type.

 

                                                                                                            CSharpInterfaceExp.zip (41.33 kb)