Differences between interfaces and classes in C #

Source: Internet
Author: User

1, the interface is similar to a class, but the members of the interface are not executed, it is only a combination of methods, properties, events and indexers, and can only contain these four types of members, in addition to these four members can also be other members (such as fields).
2. An interface is not instantiated, the interface includes only the signature of the member, and the class can be instantiated (except for the abstract class).
3, the interface does not have a constructor, class has a constructor function.
4. An interface cannot be overloaded with operators, and classes can be overloaded with operators.
5. A member of an interface has no modifiers, its members are always public, and a member of a class can have modifiers (e.g., virtual or static).
6. A class that is derived from an interface must implement the way all members in the interface are executed, but not from the class.
So why should there be interfaces. The main reason is that it is an effective contract. Class has some members that do not consider grouping these members together, a class is just a list of methods, fields, and properties, but in order to be able to use a class in some way, you must know that a class can perform those functions by declaring a class that executes one or more interfaces, and the way the class executes the interface is derived from the interface, Provides a way to execute all members defined in this interface.

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.