Interface in C#

What is Interface?


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

So, what are interfaces good for if they don't implement functionality? They're great for putting together plug-n-play like architectures where components can be interchanged at will. Since all interchangeable components implement the same interface, they can be used without any extra programming. The interface forces each component to expose specific public members that will be used in a certain way.

Example

 //Interface
    class Interface_Example
    {
        static void Main(string[] args)
        {
            College c = new College();
            Console.WriteLine(c.StudentName());
            Console.WriteLine(c.TeacherName());
            Console.ReadLine();
        }
    }
   
    public interface Student
    {
        string StudentName();
        string TeacherName();
    }
    
    public class College:Student
    {
        public string StudentName()
        {
            return "John";
        }
        public string TeacherName()
        {
            return "Dr Sara.";
        }
    }

No comments:

Post a Comment

Please let me know if you have any feedback or doubts.

Jagged Array

What is Jagged Array? Jagged array is called as "array of arrays". when you need a data inside your array element at that time you...