Users Pricing

blog

Home Blogs Interface in C# – MindStick

Interface in C#

Uttam Misra 3973 27 Dec 2010 Updated 18 Sep 2014

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

Uttam Misra

Information Technology

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


Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md