Introducing interfaces in C#

巴扎黑
Release: 2017-09-06 14:29:31
Original
1382 people have browsed it

For many beginners, it is easy to get confused. It is very simple to use. It is nothing more than defining an interface. The interface contains some attributes, indexers, events and some methods without modifiers, and there is no specific implementation of the methods. code; then inherit the interface in the class and implement the specific implementation code of all properties, indexers, events and methods in the interface (actually there are only these few in the interface, generally we only use properties and methods, so in Here I will only share one of the "methods" with you). The use of interfaces is so simple, just follow it and write code; but people who don't really understand the role of interfaces will feel that there is no need to use interfaces; it feels like it is superfluous. I was the same way when I didn't really understand the role of interfaces. thought.

Microsoft will not put unnecessary things on the market, so let’s take a look at the beauty of the interface.

We first define an interface

public Interface IBase
      {
            void ClassPrind(string s);
      }
Copy after login

Then we define a class that inherits from the IBase interface and implements the method

 public class DogClass :IBase
      {
             public void ClassPrind(string s)
             {
                   Console.WriteLine(s);
             }
      }
Copy after login

Finally we call it in Main

 class ClassMain
     {
          statric void Main(string []args)
         {
                IBase base=new DogClass();
                base.ClassPrind("小狗");
         }
     }
Copy after login

Output result

From the above code alone, it seems that it is superfluous. We only need to instantiate the DogClass class and call the ClassPrind method. There is no need to define an additional interface. But if we want to write another CatClass class, what if we also need to pass in a parameter output method in the class? Yes, it is very fast if we only need to write one more class and one more method in the class; but the method names in the newly written class may be different, which means we need one more method; this is nothing, If your project manager asks someone else to write this class and method, you will not know this method and it will be inconvenient for us to use it. But it's different if we use an interface. Although we still have to write one more class and one method, we only need to inherit this class from the interface and then implement it. So no matter who adds this class, we can accurately find him and use him.

Let’s add the CatClass class

public class CatClass :IBase
      {
             public void ClassPrind(string s)
             {
                   Console.WriteLine(s);
             }
      }
Copy after login

Let’s look at the call and output

class ClassMain
     {
          statric void Main(string []args)
         {
                IBase base=new DogClass();
                base.ClassPrind("小狗");
                IBase base=new CatClass();
                base.ClassPrind("小猫");
         }
     }
Copy after login

The output result

Puppy

Small Cat

From the call output, we can clearly see that as long as we change the class name and parameters, the result of the call is a method in another class. We don't need to remember the method name written by others. Nothing, there is no need for the person who wrote this class to tell us what this class is written for. In this way, we can easily expand whether we are in a team project or a personal project, greatly improving work efficiency. We don't have to remember so many things, so why not do it.

The above is the detailed content of Introducing interfaces in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template