blog

Home / DeveloperSection / Blogs / Interface in C#

Interface in C#

Uttam Misra3455 27-Dec-2010

An interfacelooks like a class, but has no implementation. The only thing it contains isdefinitions of events, indexers, methods and/or properties.The reason interfaces onlyprovide definitions is because they are inherited by classes and structs,which must provide an implementation for each interface member defined.

Example
//defining first interface
    interface Ifirst
    {
        voidsum(int a, intb);
    }
 
//definingsecond interface which inherits first interface
    interface Isecond:Ifirst
    {
        voidsub(int a, intb);
    }
 
//definingclass which inherits second interface
 
    class newClass : Isecond
    {
//implementinginterface defined methods
        public void sum(int a, int b)
        {
            MessageBox.Show("Sum is: " + (a + b).ToString());
        }
        public void sub(int a, int b)
        {
            MessageBox.Show("Difference is: " + (a - b).ToString());
        }
    }

Updated 18-Sep-2014
More than 18 years of working experience in IT sector. We are here to serve you best.

Leave Comment

Comments

Liked By