Interfaces in C#

In the previous chapters, we saw intangible classes. Interfaces are very abstract and they share the fact that none of them can be made. However, interfaces are even more conceptual than abstract classes because no method bodies are allowed. So an interface is like an abstract class, but nothing, but with abstract methods, and because there are no methods with the actual code, though there is no need for indicators and events along with permission. An interface can be considered as an agreement - it is important to implement a class to implement all methods and properties, however, the most important difference is that That is when C # allows multiple succession, where the classes gain more than a single base class, then it actually allows the implementation of several interfaces!

So, how does all this look in code? Here's a perfect example, take a look, maybe you see yourself, and then read for full details:


using System;
using System.Collections.Generic;

namespace Interfaces
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Dog> dogs = new List<Dog>();
            dogs.Add(new Dog("Fido"));
            dogs.Add(new Dog("Bob"));
            dogs.Add(new Dog("Adam"));
            dogs.Sort();
            foreach(Dog dog in dogs)
                Console.WriteLine(dog.Describe());
            Console.ReadKey();
        }
    }

    interface IAnimal
    {
        string Describe();

        string Name
        {
            get;
            set;
        }
    }

    class Dog : IAnimal, IComparable
    {
        private string name;

        public Dog(string name)
        {
            this.Name = name;
        }

        public string Describe()
        {
            return "Hello, I'm a dog and my name is " + this.Name;
        }

        public int CompareTo(object obj)
        {
            if(obj is IAnimal)
                return this.Name.CompareTo((obj as IAnimal).Name);
            return 0;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
}

Let's start in the middle, where we declare the interface. As you can see, the only difference is that class declaration, the keyword used - interface instead of class Also, the name of the interface is prefixed with one for interface - it's just a coding standard, and it is not necessary, you want your interface, but because they are used very much for classes if your Have a hard time nearby to show a difference in some parts of your code, I think the prefix is ​​very good

After this, we declare the description method, and later, the property of the name, in which both meet and have a set of keywords, making it a read and writeable asset. You will also see lack of access modifiers (public, private, protected etc.), and this is because they are not allowed in the interface - these are all public by default

Next is our dog class, note how it looks that with the name of the class and the class / interface is being sub-treated with the colon, however, in this case, one of the two interfaces There is only room, which is separate from commas, you can apply as many interfaces as you want, but in this case we implement only two - our own IAnimal interface and NetIsemperables etc. Interface, which is a shared interface for sections that can be sorted. Now as you can see, we have implemented both method and property compared to the IONIML interface as well as the IComperbal interface.

Now you can think: If we have to do our work completely, then why are you troubled by applying all the methods and qualities? And this is a good example of why it's worth your time, at the top of our example here, we add a bunch of dog items to a list, and then we sort the list and how it shows that dogs How to sort? Because our dog is a comparative method of class that can tell how to compare two dogs. And how does this list know that the purpose of our dog can do the same, and what method can be said to compare the dog? Because we implemented it by implementing an interface which promises a comparative method! This is the real beauty of the interfaces.