Jitesh Byahut 4/28/2015 4031

Interface In C#

Interface is not a class, it is an entity but it is similar to class that contains abstract methods, properties, indexers and events. Interface has no implementation, it has only definitions. If a class implements an interface, then interface agrees to provide the all features define by the interface and class agrees to implements all features provide by interface.

In C# multiple inheritance can be achieved by interface. If an abstract class has only abstract members, means only signature then it is equals to interface.

Features of Interface:

1. Interface is simply defined by interface keyword.

2. Interface has no implementation it has only the signature. Interface have only abstract method.

3. Interface cannot have access modifier private, protected, protected internal.

4. Interface cannot have data field.

5. The class must implements all features of inherited interface.

6. A class implement more than one interface but only one abstract class inherit.

An Interface is not a class, it is an entity that is defined by the keyword interface.

interface IAdd

{

       

}

Interface has no implementation it has only the signature. All methods inside interface is abstract method.

interface IAdd

{

    int Add(int a, int b);

}

By default interface is public. Its support only public and internal access modifiers not private, protected, protected internal.

// Incorrect

private interface IAdd{ }

protected interface IAdd{ }

protected internal interface IAdd{ }


// Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal

Interface can not contains data fields.

// Incorrect

interface IAdd

{

   int a;        

}

// Interfaces cannot contain fields

If a class implemented an interface, it agree to provide the feature defined by interface. If you not implement then it's throwing error

// Incorrect

// It’s throwing an exception that forcing to implement interface’s method

 

    interface IAdd

    {       

        void Add(int a, int b);

    }

   

    class Program : IAdd

    {

        static void Main(string[] args)

        {

           

        }       

    } 

// 'Interface.Program' does not implement interface member 'Interface.IAdd.Add(int, int)

A simple example of Interface IAdd.

    // Defining an interface

    interface IAdd

    {

        // Abstract method

        void Add(int a, int b);

    }

   

    class Program : IAdd

    {

        static void Main(string[] args)

        {

            Program pro = new Program();           

            IAdd objadd = pro;

            // Access interface's method

            objadd.Add(4, 5);

 

            Console.Read();

        }

 

        // Implement interface method ‘IAdd.Add’

        public void Add(int x, int y)

        {

            Console.WriteLine("Interface IAdd.Add method - " + x + y);

        }       

    }

   

    // Output

    // Interface IAdd.Add method - 45

Interface can contain properties.

    // Defining interface

    interface IAdd

    {

        int first_number { get; set; }

        int second_number { get; set; }

        void Add(int first_number, int secon_dnumber);

    }

   

    class Program : IAdd

    {

        // Implementing interface's properties

        public int first_number { get; set; }

        public int secon_dnumber { get; set; }

        static void Main(string[] args)

        {

            Program pro = new Program();           

            IAdd objadd = pro;

 

            // Access interface's members

            objadd.first_number = 5;

            objadd.secon_dnumber = 4;

            objadd.Add(4, 5);

 

            Console.Read();

        }

 

        // Interface IAdd.Add method

        public void Add(int x, int y)

        {

            Console.WriteLine("Interface IAdd.Add method - " + (x + y));

        }       

    }

 

    // Output

    // Interface IAdd.Add method - 9

A class can inherit more than one interface.

    // Defining first interface

    interface IAdd

    {

        void Add(int a, int b);

    }

   

    //Defining second interface

    interface IMultiply

    {

        void Multiply(int a, int b);

    }

    class Program : IAdd, IMultiply

    {

        static void Main(string[] args)

        {

            Program pro = new Program();           

            IAdd objadd = pro;

            //Access interface IAdd's method

            objadd.Add(4, 5);

 

            IMultiply objmultiply = pro;

            //Access interface IMultiply's method

            objmultiply.Multiply(4, 5);

            Console.Read();

        }

        // Interface IAdd.Add method

        public void Add(int x, int y)

        {

            Console.WriteLine("Interface IAdd.Add method - " + (x + y));

        }

        // Interface IMultiply.Multiply method

        public void Multiply(int x, int y)

        {

            Console.WriteLine("Interface IMultiply.Multiply method - " + (x * y));

        }

    }

 

Difference between abstract and interface

1. A class may inherit only one abstract class, but a class may inherit several interfaces.

2. Abstract class has may be abstract and non- abstract members, but interface have only by default abstract members.

3. Abstract class may contain access modifier but interface cannot contain access modifier.

4. Abstract class can have fields and constants, but interface has no fields define.

5. An Abstract class cannot provide multiple inheritance, but an interface provides multiple inheritance in C#.

6. By default Abstract class is non-public, but by default Interface is public.



Please give your feedback for improving this page