C # extern modifier Overview

Source: Internet
Author: User

    This section describes how to use the C # extern modifier and DllImport together with the static modifier. It can also be used for calling components of different versions of the same assembly and cannot be used together with the abstract modifier.

     

    C # is a common language. Here we mainly introduce the C # extern modifier, including the member functions that are often used to call system API functions.

     

    What does the C # extern modifier mean?

     

    The C # extern modifier is used to declare that member functions implemented outside the Assembly are often used to call system API functions (through DllImport ). Note: when using the C # extern modifier and DllImport together, you must add a static modifier. It can also be used for calling components of different versions of the same assembly (using extern to declare aliases) it cannot be used together with the abstract modifier.

     

    Example:

     

      
      
    1. Using System;
    2. Using System. Collections. Generic;
    3. Using System. Text;
    4. Using System. Runtime. InteropServices;
    5. Namespace Example03
    6. {
    7. Class Program
    8. {
    9. // Note that DllImport is an Attribute Property,
    10. // Defined in the System. Runtime. InteropServices namespace
    11. // A static modifier must be added when the extern and DllImport are used together.
    12. [DllImport ("User32.dll")]
    13. Public static extern int
    14. MessageBox (int Handle, string Message, string Caption, int
    15. Type );
    16. Static int Main ()
    17. {
    18. String myString;
    19. Console. Write ("Enter your message :");
    20. MyString = Console. ReadLine ();
    21. Return MessageBox (0, myString, "My Message Box", 0 );
    22. }
    23. }
    24. }

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.