CLR. via. C # Part 2 Section 13th interface Reading Notes (7 ),

Source: Internet
Author: User

CLR. via. C # Part 2 Section 13th interface Reading Notes (7 ),

This chapter is a basic cognitive knowledge.

One of the important points is the benefits of generic interfaces (in fact, it is also one of the benefits of using generic interfaces): type security during compilation & reduce packing when processing value types.

I don't have one in the book. Originally, I would like to talk about this knowledge separately in C #. Here I will explain it separately.

Interface callback

In fact, I cannot figure out why this book doesn't talk about the concept of interface callback. I don't believe that interface callback is only used in java.

So far, I suddenly found that many basic programming concepts are common in object-oriented programming languages, but I'm sorry, I don't have them in C # (related books! However, I believe that you will have a great chance to see the detailed explanation of these concepts when reading Java books.

Fortunately, I have also learned Java.

Until now, I have rarely seen the concept of "interface callback" in C # books. For beginners who have been familiar with C # At the beginning, it is really difficult to understand the behavior of some objects if many basic programming concepts are left blank.

I began to doubt the height of C # book writers.

Interface callback is also a manifestation of polymorphism.

First, use a piece of code to call back the interface:

The code for the interface IMyInterface1 is as follows:

namespace CATest101{    interface IMyInterface1    {        void Method1();    }}

Class MyClass1, implementation interface IMyInterface1, code (Omitted namespace) is as follows:

Public class MyClass1: IMyInterface1 {public void Method1 () {Console. WriteLine ("method in MyClass1: Method1 ()");}}

Code in the Main method (interface callback ):

Static void Main (string [] args) {MyClass1 my = new MyClass1 (); IMyInterface1 I = my; // declares that the interface type Pointer Points to the instance object I of MyClass1. method1 (); Console. readLine ();}

Output:

As shown in, interface variable I calls the Method1 method. Actually, the Method1 method in the subclass MyClass1 is called.

This is the interface callback. The interface callback introduces another similar concept:

Upward Transformation

The upward transformation is that the parent class references the code expression that points to the subclass object, that is, similar to Father f = new Child.

It is assumed that the parent class subclass has its own myMethod method,

Call f. myMethod (); If myMethod in the parent class is a virtual method, myMethod of the subclass is called (where the subclass must be overwritten using override). If myMethod of the parent class is not a virtual method, then f. myMethod calls the internal myMethod method.

"Simple factory Mode"Is the principle of upward transformation.

Finally, an encyclopedia is used:

Interface reconciliation and upward transformation are the core decoupling of the design model. It can be said that almost all models are built on the application of the two.

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.