One of the C # static and dynamic combination programming: The constraint strength of interfaces and delegates

Source: Internet
Author: User

There are two mysterious elements in the world of programming, which are ubiquitous and often undetected. They move a quiet, but they live in harmony. I gave the brothers a bad name, one called "agreement" and one called "constraint." We often see the dynamic language, static language behind, is essentially "protocol" and "constraint" the role of the two elements. Static language and dynamic language do not have a clear boundary, they have advantages and disadvantages.

C # is a beautiful language, it combines the advantages of static and dynamic, if used properly, must be able to combine dynamic and static, showing a harmonious beauty. Especially. NET platform and the rapid development of language, but also shows the dynamic combination of static and dynamic programming. This article is. NET static and dynamic combination of programming the first article, I hope this series can discuss with you how to. NET platform to maximize the potential of the combination of static and dynamic. I'm just a beginner of. NET, the understanding of computer theory is still very superficial, the article in error welcome criticism, deficiencies Welcome to add, thank you!

Ignored protocols

When it comes to "protocol", it may be the TCP/IP protocol stack that first emerges in our minds, but we actually deal with it everywhere. In the following example, do you see the agreement coming?

B(){
    ArrayList lst = A();
    foreach (string item in lst){
        Console.WriteLine(item.Length);
    }
}

Method B assumes that method a follows: All returned ArrayList are internally string-type elements. This is the protocol between them, which is not bound by the compiler static check. Therefore, the protocol implies runtime uncertainty, and method A is entirely likely to load a non-string element in the return result, which causes B to produce an exception at run time.

. NET2.0 adds static type constraints through a generic collection:

B(){
    List<string> lst = A();
    foreach (string item in lst){
        Console.WriteLine(item);
    }
}

In this way, b no longer has to worry about the existence of non-string elements within the LST, all of which benefit from a static type constraint that the generic type is a plus.

Constraints have strong and weak

Constraints have strong or weak points. The stronger the constraint, the stronger the static, the greater the support of the compiler; Conversely, the weaker the constraint, the stronger the dynamic, the greater the flexibility of the runtime.

Often see a discussion of the similarities and differences between the single method interface and the delegate, many friends think they are completely equivalent. In fact, they have markedly different constraint intensities. An interface is a static type constraint, and a delegate is a static signature constraint, and the strength of the two is completely different. In other words, the delegate has more protocol, and can be invoked as long as the signature is met, and the object that can be invoked by the interface must implement the 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.