SoFunction
Updated on 2025-04-08

Let’s talk about C# interface issues. Beginners can watch it quickly

Projects during this period have used interfaces. At first, they didn’t understand interfaces in particular, but they just knew that the interface definition was very simple, and even thought that this interface was just an extravagant move (during personal development). Now that we start team development, we realize that the interface is so important and convenient!

Next, let’s talk about my brief insights on the use of interfaces during this period. I hope everyone will like it. If you have any incorrect words, I hope everyone will give me some suggestions!

READY GO! 

I won’t talk about the definition of the interface. It has a very important knowledge point, that is, all inheriting this interface class must implement the definition in the interface. Speaking of this, in team development, as long as we agree on the interface, will our code be unified? ! !

This is the first important point of the interface: it facilitates us to unify project regulations and facilitates the management of team code!

Let’s use another example to illustrate:

Company A decided to develop an animal system that contains many animals, and the company decided to realize the shouting behavior of each animal...
Speaking of this, we usually start to do things drastically after we get the animals we want to implement! ! !
X programmer implements the dog class, he writes a shouting method void Han(){...}
Y programmer implements the cat class, he writes a shouting method void Shout(){...}
M programmer implements the pig class, he writes a shouting method void Shout(string content){...}
……………… 

Okay, now all the animals that each needs to complete have been completed, and the old king next door has begun to realize the sound of all the beasts! ! ! ! &¥%¥*%¥¥%¥ swearing! How to write this? Go to call one by one? ? ?

Let’s take a look. X programmer’s English is not very good, and he doesn’t care too much about it. He just writes out how animals shout. The names of the shouting methods written by Y programmers and M programmers are the same, but M programmers also need to convey the content of animals shouting! ! ! ! !

The next door Lao Wang now wants to call all the animals once, and he has to call the method one by one...

OK, next meeting will discuss, Lao Wang next door defines an animal interface, and all animal classes must inherit this interface, and this interface only defines a void Shout(); (It's not too much to write, just lazy)

After the programmer of X, Y, M inherited, X, M immediately found something wrong and began to change the class in his hands.

At this time, Lao Wang started to ring with all the beasts! Hahahahahaha

Next post the code for everyone to read

Interface

using System;
using ;
using ;
using ;
using ;

namespace InterfaceProject
{
  /// <summary>
  /// Animal Interface  /// </summary>
  interface IAnimal
  {
    /// <summary>
    /// Animals scream    /// </summary>
    void Shout();
  }
}

dog

using System;
using ;
using ;
using ;
using ;

namespace InterfaceProject
{
  /// <summary>
  /// dog  /// </summary>
  public class Dog:IAnimal
  {
    public void Shout()
    {
      ("Woom wool");
    }
  }
}

cat

using System;
using ;
using ;
using ;
using ;

namespace InterfaceProject
{
  /// <summary>
  /// cat  /// </summary>
  public class Cat:IAnimal
  {
    public void Shout()
    {
      ("Meow Meow");
    }
  }
}

pig

using System;
using ;
using ;
using ;
using ;

namespace InterfaceProject
{
  /// <summary>
  /// pig  /// </summary>
  public class Pig:IAnimal
  {
    public void Shout()
    {
      ("Why is the pig calling?? The pig calling");
    }
  }
}

The next door Lao Wang came to realize the sound of all beasts (destroy the existence of a person like Lao Wang)

using System;
using ;
using ;
using ;
using ;

namespace InterfaceProject
{
  class Program
  {
    static void Main(string[] args)
    {
      //A hundred beasts are ringing together (reflection can be used here to initialize all animals that inherit IAnimal. I won’t write this, mainly depends on the interface)      List<IAnimal> animals = new List<IAnimal>();
      IAnimal dog = new Dog();
      (dog);
      IAnimal cat = new Cat();
      (cat);
      IAnimal pig = new Pig();
      (pig);
      //Call all animals once      for (int i = 0; i < ; i++)
      {
        animals[i].Shout();
      }

      
    }
  }
}

I'll finish my rough insights on this interface! Although the interface is very simple to use, we still need to understand the role of this interface. I hope this article will allow more novices like me to take the first step towards the interface.