Search This Blog

Sunday, 7 July 2019

Interface in C#

Interface

* We create a interfaces using interface keyword. Just like classes interfaces also contains properties, methods, delegates or events, but only declarations and no implementations.

 It is a compile time error to provide implementations for any interface member.

* Interface members are public by default, and they don't allow explicit access modifiers.

 Interfaces cannot contain fields.

* If a class or a struct inherits from an interface, it must provide implementation for all interface members. Otherwise, we get a compiler error.

A class or a struct can inherit from more than one interface at the same time, but where as, a class cannot inherit from more than once class at the same time.

* Interfaces can inherit from other interfaces. A class that inherits this interface must provide implementation for all interface members in the entire interface inheritance chain.

We cannot create a instance of an interface, but an interface reference variable can point to a derived class object.

Interface Naming convention. Interface names are prefixed with capital!

Interface Sample program:

namespace @interface
{
    //Interface is only for method declaration not implementation.
    interface ICustomer
    {
        void Print();
    }
    class Customer : ICustomer
    {
        public void Print()
        {
            Console.WriteLine("Interface print method");
            Console.ReadLine();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Customer c1 = new Customer();
            c1.Print();
        }
    }

}


1 comment:

  1. I am regular reader of your blog from long time,What a Beautiful post! This is so chock full of useful information I can’t wait to dig and start using my time on blogging and I am looking for and I love to post a comment that "The content of your post is awesome" Great work!
    Java Training in Chennai

    Java Training in Velachery

    Java Training in Tambaram

    Java Training in Porur

    Java Training in Omr

    Java Training in Annanagar

    ReplyDelete