Home > DeveloperSection > Blogs > Interface in C#

Interface in C#


C# C# 
Ratings:
0 Comment(s)
 1934  View(s)
Rate this:

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());

        }

    }

Don't want to miss updates? Please click the below button!

Follow MindStick