Understanding of interfaces in c #

Source: Internet
Author: User

In my understanding, an interface is used to identify the type of a class.

For example, if there is an Interface: an animal, whether it is the class: cat, dog, ox, etc,

As long as it inherits the animal interface, it is necessary to implement the method defined by the animal interface.

At the same time, as long as we see that a class inherits the animal interface, we can boldly call the method defined by the interface.

In addition, a class in c # can inherit multiple interfaces, that is, a class can have multiple types.

For example, a person may be a parent's son, a wife's husband, or a company employee.

Importantly, if there are many small classes in the project, and some classes have something in common (methods, attributes, etc ),

We can define an interface for these classes to inherit.

In this way, when classes A, B, and C all inherit the same interface, we only need to be familiar with the use of one of the classes,

It is very convenient to use other classes.

However, when the project is implemented in a single way, that is to say, defining an interface can only be implemented by a class.

When other classes are not used, we do not need to use interfaces.

This will only increase the complexity of the Code.

Therefore, when designing the project architecture, we should consider the situation and then use the interface.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.