15th Chapter interface [". NET Framework Program design" Reading notes]

Source: Internet
Author: User
Tags definition inheritance modifiers
. NET Framework | notes | procedure | Design Chapter 15th interface

Summary:

Interface application and fully qualified name method to define the application of the interface.



First, interface and inheritance

L C # Support single implementation inheritance and multiple interface inheritance

The L interface can be defined as: event, parameterless property (property), parameter-containing property (indexer); C # does not allow interfaces to define any static members (the CLR allows static members to be defined); The CLR does not allow interfaces to define instance fields and constructors.

L default to public abstract methods, but not decorated with any modifiers (including public)

L Converts a value type to an interface type (assuming it implements an interface), the value type is boxed as a reference type to invoke the interface method in it.



The principle of implementing an interface inheritance or implementing inheritance:

L exist is-a relationship using implementation inheritance, existence can-do relationship using interface inheritance

Implementation inheritance inherits the implementation of the parent type, since the implementation of the method is not defined in the interface, so the method must be implemented after inheritance

L The implementation of the same part of the function can be implemented by using the implementation of the interface, considering the different implementations of each

L Adding a method to a parent type may not affect the use of a user inherited from that type, and adding a method to an interface causes the user to add an implementation to the new method

Change the field in the boxed type with the interface

Instead, the boxed type is transformed into an interface type, and then the value is changed by calling the method.

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



Iii. implementing multiple interfaces with the same method

Use the interface name. The form declaration of method name implements the method of that interface. becomes the fully qualified interface name, which is considered a private method and cannot be invoked using the type itself, and when the type is converted to an interface type, the fully qualified method of the interface name definition becomes a public method. (More peculiar and useful); A method defined with a fully qualified interface name cannot use any access modifiers.



Iv. Display Interface member implementation

The problem of the proposed: the interface is usually accepted by the parameters of object, such parameters are not strongly type-safe, may need to be in our custom type of interface implementation of the definition of strong type security method, but also need to implement "interface contract"

Problem solving: Through the above fully qualified interface name to define the interface method, can achieve this effect.

Advantage: Strong type support is available to discover type mismatch issues at compile time, not to runtime. At the same time, invoking a custom method eliminates the need for boxing and improves efficiency.

Disadvantage: This method should be used with caution because we hide the interface implementation in the type (the method that must be transformed into an interface to expose the interface definition), and therefore may be inconvenient to use.


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.