For many beginners is a very easy to confuse, it is very simple to use, is simply to define the interface, the interface contains some properties, indexers, events and some methods without modifiers, there is no method of implementation of the code, and then inherit the interface in the class, implement all the properties in the interface, indexers, The specific implementation of the event and method code (in fact the interface only these few, generally we use only properties and methods so here I just share one of the "method"). The use of the interface is so simple, as long as the code to write the line, but no real understanding of the role of the interface, will feel the need to use the interface, feel superfluous, I do not really understand the role of the interface, I also think so.
Microsoft is not going to put superfluous stuff on the market, so let's take a look at the beauty of the interface.
Let's define an interface first.
Public Interface IBase { void Classprind (string s); }
The definition of a class inherits from the IBase interface, and the implementation method
public class Dogclass:ibase {public void Classprind (string s) { Console.WriteLine (s); } }
Finally, we call him in main.
Class Classmain { statric void Main (string []args) { IBase base=new dogclass (); Base. Classprind ("Puppy");} }
Output results
Little Dog
Just from the code above to see indeed superfluous, we only need to instantiate the Dogclass class to invoke the Classprind method on the line, there is no need to define a more interface. But what if we were going to write more than one Catclass class, and the same way we were going to pass a parameter output to the class? Good we just write more than one class, the class to write more than one method is fast, but we in the new class of the method name may be different, that is, we have one more method, this is not what, if this class and method your project manager to write, you will not know this method, we use it is inconvenient. But if we use the interface is different, although also to write more than one class and a method, but we simply inherit this class from the interface, and then implement him. No matter who is going to add this class, we can find him exactly and use him.
Let's add catclass to this class.
public class Catclass:ibase {public void Classprind (string s) { Console.WriteLine (s); } }
Then look at the call and the output
Class Classmain { statric void Main (string []args) { IBase base=new dogclass (); Base. Classprind ("Puppy"); IBase base=new Catclass (); Base. Classprind ("Kitten");} }
Output results
Little Dog
Kitten
From the output of the call we clearly see that we just change the class name, change the result of a parameter call is not a method in a class, we do not have to remember what other people write the method name, do not have to write this class to tell us what this class is written to do. So we are in the team project, or in the individual projects are easy to expand, greatly improve the efficiency, we do not have to remember what many things, why not.