This is our Lesson No 22 and today I will teach you about Learning to work with interfaces in #C.
Work with interfaces in #C
We looked at Basic and Abstract classes in C # in the last course. C# interfaces are also available. It is very similar to Class Abstract in that it is not possible to make a clone or object from it, and they are identical.
Because of the substance of the commands, interfaces are even more conceptual than Abstract classes. You cannot send commands for defined methods (body methods are not allowed). Because the interfaces don’t have any real-code methods, fields aren’t required.
But it is possible to specify the properties of properties, indexers and events. You can also think of an interface as a contract It requires the implementation of all methods, methods and properties of the class.
Note: The important thing about interfaces is that since C # can inherit multiple (does not allow inheritance, ie one class to inherit from more than one base class, but allows Interface Gives multiple.
But what will all this be said in the code? The following example code is a good example of interfaces Is #C. Take a look at the example code, we will explain about it below:
Let’s start with the middle of the code, where we defined the Interface. As you can see, the only difference In defining an interface instead of class, a keyword is used that the interface is written instead of class Is.
Also, before the name Interface put an I to consider the expression interface, which is just a standard It is coded and does not need to be observed. You can program your desired interface anywhere Ask to call, and since calling them is similar to calling classes, put I before the name The interface helps the coder to better understand the code.
Then define the Describe method and then write the Property Name property that has both words The get and set key is meant to be a write and read property.
Important Note: As you can see, the properties that determine the field of view or access level (modifiers) such as public, private, protected, etc. are not placed at the beginning of the Interface name. Because These properties are not allowed in the interfaces and they are all public by default.
In the next step, we put the class Dog. Then by inserting one: between the class name and the case interface Comment, the Dog class is inherited from the Interface, just like the classes. But in the above code there are two interfaces for We have used the Dog class and separated them by commas. You can use any number of interfaces You want to define it in front of the class name, but in this example we only have two interfaces called Animal and We attribute IComparable to our class.
IComparable is a common interface between classes that They have the ability to sort.
Conclusion, in the above code we also have a method and a Use the property from the IAnimal Interface in the code and at the same time the To Compare method from IComparable We have used the interface.
Now you may be thinking to yourself, if we have to do everything ourselves and all the methods and properties
Properties Define the need in the code, why bother so much to write the Interface.
You can see the answer to the above question well in the upper part of the example code and understand why it takes so long Laying will be valuable.
We define a number of Dogs as objects in a list in the sample code above, and then sort our list. However, how does list know how to rank dog names? Because of this, the Dog Class It provides a CompareTo function that explains how to compare two Dogs.
But how does it know which object to list? Which method should the dog use for the Compare dogs call if it can execute the sorting operation? We have informed the class and the list since we do this by choosing an acceptable interface and assigning it. The CompareTo method should be used by the interface. The interfaces are written in code.