C# extern修饰符概述

    这里介绍C# extern修饰符和DllImport一起使用时要加上 static 修饰符也可以用于对于同一程序集不同版本组件的调用,不能与 abstract 修饰符同时使用。

    C#语言还是比较常见的东西,这里我们主要介绍C# extern修饰符,包括介绍成员函数经常用于系统API函数的调用等方面。

    C# extern修饰符是什么意思?

    C# extern修饰符用于声明 由程序集外部实现的成员函数经常用于系统API函数的调用(通过 DllImport )。注意,C# extern修饰符和DllImport一起使用时要加上 static 修饰符也可以用于对于同一程序集不同版本组件的调用(用 extern 声明别名) 不能与 abstract 修饰符同时使用 。

    示例:

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Text;  
    4. using System.Runtime.InteropServices;  
    5.    
    6. namespace Example03  
    7. {  
    8. class Program  
    9. {  
    10. //注意DllImport是一个Attribute Property,  
    11. //在System.Runtime.InteropServices命名空间中定义  
    12. //extern与DllImport一起使用时必须再加上一个static修饰符  
    13. [DllImport("User32.dll")]  
    14. public static extern int   
    15. MessageBox(int Handle, string Message, string Caption, int   
    16. Type);  
    17. static int Main()  
    18. {  
    19. string myString;  
    20. Console.Write("Enter your message: ");  
    21.  myString = Console.ReadLine();   
    22. return MessageBox(0, myString, "My Message Box", 0);  
    23. }  
    24. }  
原文地址:https://www.cnblogs.com/zcy_soft/p/1773628.html