C# extern关键字

        修饰符用于声明在外部实现的方法。extern修饰符的常见用法是在使用Inpterop调入非托管代码时与DllImport属性一起使用;在这种情况下,该方法还必须声明为static。

        extern关键字还可以定义外部程序集别名,使得可以从单个程序集中引用同意组件的不同版本。

        extern不能喝abstract一起使用,使用extern修饰符意味着方法在C#代码的外部实现,而使用abstract意味在类中提供方法实现。

class Program
    {
        [DllImport("User32.dll")]
        public static extern int MessageBox(int h,string m,string c,int type);
        static void Main(string[] args)
        {
            string myString;
            Console.WriteLine("Enter your message:");
            myString = Console.ReadLine();
            int a = MessageBox(0,myString,"My Message Box",0);
            Console.ReadLine();
        }
    }

猜你喜欢

转载自blog.csdn.net/luochenlong/article/details/80446261