SoFunction
Updated on 2025-03-08

C# briefly understands how to use interface (Interface)

Inheriting "base class" and inheriting "interface" can achieve some of the same functions, but some interfaces can complete the functions that cannot be achieved by using only the base class.

1. The interface is used to describe the public methods/public properties of a set of classes. It does not implement any methods or properties, but just tells the class that inherits it. At least, what functions to implement. The class that inherits it can add its own methods.

2. Use an interface to make the class that inherits it: name uniform/standard, easy to maintain. For example: two classes "dog" and "cat", if both inherit the interface "animal", and there is a method Behavior() in the animal, then the dog and cat must implement the Behavior() method and both are named Behavior so that the naming will not occur. If the naming is not Behavior(), the interface will be constrained, that is, the compilation will not be passed without naming according to the interface constraints.

3. Provide a permanent interface. When the class is added, the existing interface methods can meet most methods in the inherited class. There is no need to redesign a set of methods for the new class, which also saves code and improves development efficiency.

Let's give a code example:

//Public interface: "animal"public Interface IAnimal 
{ 
   int EyeNumber; 
   private void Behavior(); //Behavioral methods, describing the characteristics of various animals} 
//Category: Dogpublic Dog : IAnimal 
{ 
   string ActiveTime = "daytime"; 
   private void Behavior() 
   { 
      ("I sleep at night and exercise during the day"); 
   } 
}
//Category: Catpublic Cat: IAnimal 
{ 
   string ActiveTime = "night"; 
    private void Behavior() 
    { 
         ("I sleep during the day and act in the evening"); 
    } 
} 
//Simple application:public static Main() 
{ 
    Dog myDog = new Dog(); 
    (); //Output: "I sleep at night and exercise during the day"    Cat myCat = new Cat(); 
    (); //Output: "I sleep during the day and act in the evening"}

The above call the same name method of different classes will output different things, which means that the functions completed by the same name method in each class can be completely different.

Going further, instead of using the Main method above to call the class one by one, it uses polymorphism to realize its calls.

Take a look at the following method:

public Behavior(IAnimal myIanimal) 
{ 
   (); 
}

Its parameter is <<interface type>>. Any class that inherits it can call this method. This method can call methods in different classes according to different classes. That is, it can complete the functions of different classes by itself.

Polymorphism code example:

Dog myDog = new Dog();

Cat myCat = new Cat();

Behavior(myDog); //Behavior accepts "dog" class instances

Behavior(myCat); //Behavior accepts "dog" class instances

In this way, the Behavior method can be written once to complete the different functions of all methods of the same name in the class that inherits it. It is very convenient.

Similarly, due to the functional requirements of "animal software", another "turtle" category needs to be added:

//Category: Turtlepublic Tortoise: IAnimal 
{ 
    string ActiveTime = "It's hard to say"; 
    private void Behavior() 
    { 
         ("I can stay away from activity, and I will sleep for five thousand years!"); 
    } 
}

Then you can also call the above polymorphic method, so the interface makes the method more extensible. If you inherit many classes, you can imagine how many benefits it has!

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.