13th Chapter Interface

Source: Internet
Author: User

1. Overview

This chapter discusses how to define and use an interface, and also provides guidelines to help you decide when to use an interface rather than a base class.

2. Noun interpretation

3. Main content

  3.1 Class and interface inheritance

① in the CLR, any class must derive from a class (and only a class that derives from Object).

②CLR also allows the developer to define an interface, which actually only names a set of method signatures uniformly.

3.2 Defining interfaces

For the CLR, an interface definition is like a type definition. The CLR defines an internal data structure for an interface type object, and it can use the reflection mechanism to query the functionality of the interface type.

 3.3 Inheritance Interfaces

If you implement an interface method that is not shown labeled virtual, the compiler will mark them both virtual and sealed.

  3.4 More on calling interface methods

The System.String type implements several interfaces: IComparable, ICloneable, IConvertible, IEnumerable, Icomparable<string>

Ienumerable<char>, Iequatable<string>.

  3.5 Implicit and explicit interface method implementations (what happens behind the scenes)

In C #, the name of the interface that defines the method is prefixed to the method name, creating an explicit interface method implementation (Explicit Interface methods Implementation, EIMI).

Eimi cannot specify accessibility (the default private) and cannot be marked as virtual.

 3.6 Generic interface

① compile-time type security.

② the number of packing times that are reduced when processing value types.

The ③ class can implement the same interface several times, as long as different type parameters are used each time.

 3.7 Generics and interface constraints

Generic type parameter constraints are the benefits of an interface:

① can constrain a generic type parameter to multiple interfaces.

② can reduce boxing operations when passing instances of value types.

  3.8 Implementing multiple interfaces with the same method name and signature

Must be implemented with an explicit interface method.

  3.9 Enhanced compile-time type security with explicit interface method implementations

For interfaces that do not have a generic version, an explicit interface method implementation can be implemented to enhance type safety and reduce boxing.

Internal struct somevaluetype:icomparable{    private  Int32 m_x;      Public Somevaluetype (Int32 x) {m_x = x;}      Public Int32 CompareTo (somevaluetype other)         return (M_x- other.m_x);    Int32 IComparable.CompareTo (Object other)        return  CompareTo ((Somevaluetype) other);}

  3.10 Caution using Explicit interface method implementations (EIMI)

① does not have a document explaining how a type implements a Eimi or IntelliSense support.

An instance of the ② value type is transformed into an interface fashion box.

③eimi cannot be called by a derived type.

  3.11 Design: base class or interface

①is-a vs can-do relationship.

② is easy to use. (Use base classes to make interfaces easy to use)

③ the implementation of consistency. (There is no guarantee that anyone can implement an interface exactly)

④ version control. (Adding a member to an interface forces the successor of the interface to change the source code)

4. Summary

13th Chapter 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.