Chapter 4 interfaces of. NET Framework programming

Source: Internet
Author: User

Chapter 4 Interfaces

 

Abstract:

The application of the interface and the application that defines the interface by fully qualified name.

 

I,Interfaces and inheritance

L c # support single-implementation inheritance and multi-interface inheritance

L The interface can be defined as event, no parameter property (attribute), and parameter-included property (Indexer ); C # do not allow the interface to define any static members (CLR can define static members); CLR does not allow the interface to define instance fields and constructors.

L The default value is public abstract, but any modifier (including public) is not allowed)

L convert the value type to the interface type (assuming that an interface is implemented), the value type is boxed as the reference type to call the interface method.

 

The principle of implementing interface inheritance or implementing inheritance:

L existIS-ALink use implement inheritance, existCan-doLink use interface inheritance

L Implementation inheritance can inherit the implementation of the parent type. Because there is no method defined in the interface, the method must be implemented after inheritance.

L considering the different implementations of interfaces, each user can use the implementation to inherit the implementation of the same part of functions.

L adding a method for the parent type may not affect the users inherited from this type. Adding a method for the interface requires the user to add an implementation for the new method.

II,Use the interface to change fields in the boxed type

In fact, it is to convert the boxed type to the interface type, and then change the value by calling the method.

Because the unboxing of the boxed type to the original value type will declare a new value type on the stack, the method that calls this new value type cannot change the value of the boxed type.

 

III,Implement multiple interfaces with the same method

The method of the interface is declared in the form of Interface Name and method name. The method defined in this way is considered as a private method and cannot be called by the type itself. When the type is converted to the interface type, the method that fully defines the interface name is changed to a public method. (This is odd and useful); Access modifiers cannot be used for methods that fully define interface names.

 

IV,Display interface member implementation

Question proposal: Generally, the interface accepts the object parameter. this parameter is non-strong and secure. You may need to define a method that is strongly secure in the implementation of custom interfaces, at the same time, you also need to implement the "interface contract"

Solution:You can define an interface method by using the fully qualified interface name above to achieve this effect.

Advantages: Strong type support. You can find the Type Mismatch problem during the compilation period, rather than during the runtime. At the same time, it is not necessary to bind a custom Method to Improve the efficiency.

Disadvantages:Because the interface implementation is hidden in the type (the method defined by the interface must be transformed into an interface to be exposed), this method may cause inconvenience to use, so we should use this method with caution.

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.